-5

One of our systems uses a single line for address and another splits it into two lines. So for an address like "12345 Manchester Avenue Apt 123" we need this split at 25 characters but going back to the prior space, so this string would return as:

Line1 - 12345 Manchester Avenue

Line2 - Apt 123

I'm not sure how to do this without jumping through some insane loops. Thanks.

I've tried a few loops and string functions tied together, but I'm not able to get it working as expected.

Community
  • 1
  • 1
Sam Alex
  • 79
  • 2
  • 11
  • 1
    youll need an address parsing framework – Daniel A. White Aug 21 '19 at 15:10
  • What are your rules for choosing where to split? Is it always at character 25? Is after the 3rd space. Is it after the 2nd space (backwards). Are you comfortable using RegEx? Can you give a number of examples and the desired output for each? – openshac Aug 21 '19 at 15:15
  • Show us your code. Remember that a string can appear like a collection of characters. If you have string `str`, then `str[3]` is the fourth character in the string. Combine that with `string.Length` and `string.Substring` and you should be able to cobble a solution together. `StringBuilder` may help. Don't forget to remove the space you are using as the "splitter" – Flydog57 Aug 21 '19 at 15:17
  • Split at the spaces. Add each "word" to `Line 1` while the total is less than or equal to 25. When `Line 1` + next word is greater than 25 you've got `Line 1`. Add the remaining to `Line 2`. Splitting at the spaces yield an array consisting of `["12345", "Manchester", "Avenue", "Apt", "123" ]`. Looping through the array and adding to `Line 1` yields `12345 Manchester Avenue`. (Remeber to add a space in between the words). Checking if `12345 Manchester Avenue` + space + `Apt` yields that the length is greater than 25. So stop adding to `Line 1` and add the rest of the words to `Line 2`. – Sani Huttunen Aug 21 '19 at 15:19

2 Answers2

2

One way to do it would be to find the space you'd break on, then split the string there. String.LastIndexOf has an overload that will do that for you nicely:

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
D Stanley
  • 149,601
  • 11
  • 178
  • 240
0

Here's some code that I tested in a small Windows Forms app. You can change the strings to match your names.

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);
}

It walks backwards from your "maximum length for Address1" value until it finds a space and breaks there. Getting rid of the space was the hardest part (off-by-one errors, etc.). It doesn't handle multiple spaces, and it doesn't handle arbitrary whitespace (for example, TABs). But it should do what you asked in the most obvious way.

Flydog57
  • 6,851
  • 2
  • 17
  • 18
  • I like @dstanley's answer, but it should probably be combined with mine (my initial "do we need to split at all" and my final "brute force" option. – Flydog57 Aug 21 '19 at 16:16