Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过C将地址分成两行#_C# - Fatal编程技术网

C# 通过C将地址分成两行#

C# 通过C将地址分成两行#,c#,C#,我们的一个系统使用一行作为地址,另一个系统将其拆分为两行。所以对于像“12345 Manchester Avenue Apt 123”这样的地址,我们需要将其拆分为25个字符,但返回到前面的空格,因此该字符串将返回为: 1号线-曼彻斯特大道12345号线 线路2-公寓123 我不知道如何做到这一点而不跳过一些疯狂的循环。 谢谢 我尝试了几个循环和字符串函数,但是我不能让它按预期工作。一种方法是找到要打断的空间,然后在那里拆分字符串。有一个重载,可以很好地满足您的需求: string s = "1

我们的一个系统使用一行作为地址,另一个系统将其拆分为两行。所以对于像“12345 Manchester Avenue Apt 123”这样的地址,我们需要将其拆分为25个字符,但返回到前面的空格,因此该字符串将返回为:

1号线-曼彻斯特大道12345号线

线路2-公寓123

我不知道如何做到这一点而不跳过一些疯狂的循环。 谢谢


我尝试了几个循环和字符串函数,但是我不能让它按预期工作。

一种方法是找到要打断的空间,然后在那里拆分字符串。有一个重载,可以很好地满足您的需求:

string s = "12345 Manchester Avenue Apt 123";
if(s.Length > 25) 
{
    // find the last space within the first 25 characters
    int lastSpace = s.LastIndexOf(' ',24);

    //TODO: Add logic to handle case where no space is found in the first 25 chars 

    string s1 = s.Substring(0,lastSpace);
    string s2 = s.Substring(lastSpace+1);

    Console.WriteLine(s1);
    Console.WriteLine(s2);
}

//output:

12345 Manchester Avenue
Apt 123

下面是我在一个小型Windows窗体应用程序中测试的一些代码。您可以更改字符串以匹配您的姓名

private void FixAddressBtn_Click(object sender, EventArgs e)
{
    const int maxLength = 25;
    if (LongAddressText.Text.Length <= maxLength)
    {
        Address1Text.Text = LongAddressText.Text;
        Address2Text.Text = string.Empty;
        return;
    }

    for (int i = maxLength - 1; i >= 0; --i)
    {
        if (LongAddressText.Text[i] == ' ')
        {
            Address1Text.Text = LongAddressText.Text.Substring(0, i);
            Address2Text.Text = LongAddressText.Text.Substring(i+1);
            return;
        }
    }
    //no obvious way to split it, so just brute force:
    Address1Text.Text = LongAddressText.Text.Substring(0, 24);
    Address2Text.Text = LongAddressText.Text.Substring(24);
}
private void FixAddressBtn_单击(对象发送方,事件参数e)
{
常量int maxLength=25;
if(LongAddressText.Text.Length=0;--i)
{
if(LongAddressText.Text[i]='')
{
Address1Text.Text=LongAddressText.Text.Substring(0,i);
Address2Text.Text=LongAddressText.Text.Substring(i+1);
回来
}
}
//没有明显的方法来分裂它,所以就用蛮力:
Address1Text.Text=LongAddressText.Text.Substring(0,24);
Address2Text.Text=LongAddressText.Text.Substring(24);
}

它从“Address1的最大长度”值向后走,直到找到一个空格并在那里中断。清除空间是最困难的部分(一个接一个的错误,等等)。它不处理多个空格,也不处理任意空格(例如,制表符)。但它应该以最明显的方式满足您的要求。

您需要一个地址解析框架选择拆分位置的规则是什么?总是在25号角色吗?在第三个空格之后。是在第二个空格之后(向后)。你觉得使用正则表达式舒服吗?你能给出一些例子和每个例子所需的输出吗?给我们看看你的代码。请记住,字符串可以显示为字符的集合。如果有字符串
str
,则
str[3]
是字符串中的第四个字符。将其与
string.Length
string.Substring
相结合,您应该能够拼凑出一个解决方案<代码>StringBuilder可能会有所帮助。不要忘记删除您正在使用的空间作为空间的“拆分器”拆分。当总数小于或等于25时,将每个“单词”添加到第1行。当
第1行
+下一个单词大于25时,您得到了
第1行
。将剩余部分添加到第2行。在空格处拆分产生一个由
[“12345”、“曼彻斯特”、“大道”、“Apt”、“123”]
组成的数组。在数组中循环并添加到
第1行
将产生
12345曼彻斯特大街
。(记住在单词之间加一个空格)。检查曼彻斯特大街12345号
+space+
Apt
是否产生长度大于25的结果。因此,停止添加到
第1行
,并将其余单词添加到
第2行
。我喜欢@dstanley的答案,但它可能应该与我的答案结合起来(我最初的“我们是否需要拆分”和我最后的“暴力”选项)。