如何以正确的格式打印地址? (C#WINDOWS) [英] how to print address in proper format? (C# WINDOWS)

查看:80
本文介绍了如何以正确的格式打印地址? (C#WINDOWS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string input ="Flat No-4324,印度,邦卡纳塔克邦,Distt-Banglore,Tehsil-Raipur,Ganga Institute附近,Pin-644487";

输入的字符串中可能有96个字符.

我想制作一个程序,将input.length始终除以20(input.length/20)
并打印,因此在这种情况下,输入将在第5行中打印.在第一行中,最后一个单词是-Ganga(在第一行中,它将打印-Flat No-4324,Ga附近),而在第二行中,其余的再次以-开始nga Institute .......(所以这里是问题)

我希望如果最后一个字母的长度超过长度20,那么它应该从第二行开始打印,因为我希望第一行是(Flat No-4324,Near),然后第二行从Ganga Institute开始..如果再次发生这种情况,依此类推.


如果您不满意,请采取以下解释-

我是说,如果我的每行长度都假设为11个字符,那么在打印这样的字符串时会出现一些问题->(我想踢足球,也想下棋.)
第一行将使用11个字符,即(我要p),第二行将打印(放置footbal),依此类推.因此,在这里您可以看到比赛分为2个单词p并躺下,足球又分为2个单词足球和l,所以这是一个问题.我想如果发生这种情况,那么完整的单词应该从下一行开始,因为我希望第一行是(我想要)然后第二行是(播放),在第3行(足球)中,依此类推.

plz help

string input="Flat No-4324,Near Ganga Institute, Tehsil-Raipur, Distt-Banglore,State-Karnataka Pin-644487 India";

in the input string perhaps there are 96 character.

i want to make a program by which input.length always divided by 20(input.length/20)
and print,so in this case input will print in 5 line.In first line the last word is -Ganga(here in first line it will print-Flat No-4324,Near Ga)and the remaining again in 2nd line starting with- nga Institute.......(so here is problem)

i want that if the last letter length exceeded from length 20 then it should print from 2nd line,as i want then 1st line will be(Flat No-4324,Near ) then 2nd line start with Ganga Institute..... and if again this condition occur then so on .


If u r not getting so let''s take below explanation--

i m saying that if,my every line length will be suppose 11 character,so there are some problem occur to print strings like this->( I want to play football and I want to play chess.)
1st line will take 11 character which will be(I want to p),in 2nd line will print(lay footbal) and so on.So here u can see play divide into 2 words p and lay,again football is dividing in 2 words footbal and l,so this is a problem.I want if this type of situation occur then complete word should be start from next line,as i want then 1st line will be(I want to) then 2nd line will be(play) and in 3rd line (football) and so on.

plz help

推荐答案

为什么不只使用String.Split()并在逗号处将其断开?这样做有双重好处:正确分隔地址的元素,并处理任意长度和数量的项目.
Why not just use String.Split() and break it at the commas? That will have the double benefit of separating the elements of the address correctly, and working for any length and number of items.


尝试以下操作:

Try this:

namespace myTest
{
    class Program
    {
        static void Main()
        {
            string _Input = "I want to play football and I want to play chess";
            string _ModifiedInputWithLineFeeds = StringToDefiniteLengthLines(_Input, 11);
        }

        static string StringToDefiniteLengthLines(string input, int length)
        {
            string[] words = System.Text.RegularExpressions.Regex.Split(input, "[^\\w]");
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            string NewLineCost = Environment.NewLine;
            foreach (string word in words)
            {
                string temp = sb.ToString();

                int wordLength = word.Length + 1;
                int tempLength = temp.Length;
                int totalLength = wordLength + tempLength;

                int lastindex = temp.LastIndexOf(NewLineCost);
                int intermediateLength = 0;
                if (lastindex > -1)
                    intermediateLength = wordLength + temp.Substring(lastindex).Length;

                if (intermediateLength > 0)
                    totalLength = wordLength + intermediateLength;
                if (totalLength < length)
                    sb.Append(word+" ");
                else
                    sb.AppendLine(word);
            }
            return sb.ToString();
        }
    }
}


输出:


Output:

I want to play
football
and I want
to play
chess


我建​​议您更改逻辑.
而不是在具有WhiteSpace的位置上拆分索引上的字符串.

I will suggest you to change your logic .
instead of spliting string on the index split on the where you have WhiteSpace.

string input = "Flat No-4324,Near Ganga Institute, Tehsil-Raipur, Distt-Banglore,State-Karnataka, Pin-644487, India";
            string[] Words = input.Split(',');
            char[] L1,L2,L3,L4,L5 = new char[20];
            L1[0] = L2[0] = L3[0] = L4[0] = L5[0] = ' ';
            bool P1,  P2,  P3,  P4, P5;
            P1 = P2 = P3 = P4 =P5 = true;
            int spaceleft = 0;

            foreach (string s in Words)
            {
                if (P1)
                {
                    spaceleft = 20 -L1.Length ;
                    if (spaceleft > s.Length)
                    {
                        int j = 0;
                        char[] wordchar =  s.ToCharArray();
                      for(int i = L1.Length+1;i<20;i++)
                      {
                          
                          L1[i]= wordchar[j];
                      }
                    }
                    else
                    {
                        P1 = false;
                        foreach (char c in L1)
                            Console.Write(c);
                        Console.WriteLine();
                        
                    }
                }
                else if (P2)
                {
                     spaceleft = 20 -L2.Length ;
                    if (spaceleft > s.Length)
                    {
                        int j = 0;
                        char[] wordchar =  s.ToCharArray();
                      for(int i = L2.Length+1;i<20;i++)
                      {
                          
                          L2[i]= wordchar[j];
                      }
                    }
                    else
                    {
                        P2 = false;
                        foreach (char c in L2)
                            Console.Write(c);
                        Console.WriteLine();
                    }
                }
                else if (P3)
                {
                     spaceleft = 20 -L3.Length ;
                    if (spaceleft > s.Length)
                    {
                        int j = 0;
                        char[] wordchar =  s.ToCharArray();
                      for(int i = L3.Length+1;i<20;i++)
                      {
                          
                          L3[i]= wordchar[j];
                      }
                    }
                    else
                    {
                        P3 = false;
                        foreach (char c in L3)
                            Console.Write(c);
                        Console.WriteLine();
                    }
                }
                else if (P4)
                {
                     spaceleft = 20 -L4.Length ;
                    if (spaceleft > s.Length)
                    {
                        int j = 0;
                        char[] wordchar =  s.ToCharArray();
                      for(int i = L4.Length+1;i<20;i++)
                      {
                          
                          L4[i]= wordchar[j];
                      }
                    }
                    else
                    {
                        P4 = false;
                        foreach (char c in L4)
                            Console.Write(c);
                        Console.WriteLine();
                    }
                }
                else if (P5)
                {
                    spaceleft = 20 - L5.Length;
                    if (spaceleft > s.Length)
                    {
                        int j = 0;
                        char[] wordchar = s.ToCharArray();
                        for (int i = L5.Length + 1; i < 20; i++)
                        {

                            L5[i] = wordchar[j];
                        }
                    }
                    else
                    {
                        P5 = false;
                        foreach (char c in L5)
                            Console.Write(c);
                        Console.WriteLine();
                    }
                }
                else
                {
                    Console.WriteLine("Address too long.");
                }

 
            }



我以非常可悲的方式编写了此示例代码,因此您必须对其进行重构然后再使用它.可能也有一些执行力



I wrote this sample code in very pathatic manner, so you have to refactor it and then use it. there may be some execptions too


这篇关于如何以正确的格式打印地址? (C#WINDOWS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆