0

I have address String like "abc 201 lmn road pqr near xyz building state maharashtra navi mumbai, 4212001 .... "

I want to split this string to List like

List<string> addrList = new List<string>();
addrList.add("abc 201 lmn road pqr near");
addrList.add("xyz building state maharashtra ");
addrList.add("navi mumbai, 4212001");

I tried the following function

private string[] splitToNChar(string inputString, int chunkSize)
    {

        List<string> myList = new List<string>();
        for (int i = 0; i < inputString.Length; i += chunkSize)
        {
            myList.Add(inputString.Substring(i, chunkSize));
        }
        return myList.ToArray(); 
    }

but it has an exception when I pass chunk size as 40 characters like

Exception thrown: 'System.ArgumentOutOfRangeException' in mscorlib.dll Exception eee : Index and length must refer to a location within the string.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
TejpalBh
  • 427
  • 4
  • 13
  • 3
    You have to make sure that index+length < string.Length. – Andrew Morton Apr 04 '19 at 10:35
  • what is that you're trying to achieve? split string based on space or after a specific number of characters? – Aarif Apr 04 '19 at 10:35
  • For actual methods to accomplish the task as opposed to explaining why you got that error, there are several ideas at [Splitting a string into chunks of a certain size](https://stackoverflow.com/q/1450774/1115360). – Andrew Morton Apr 04 '19 at 10:43
  • Obviously he is trying to split each number of character in this case every 40 character but the problem is after a while his string has <40 character so when application tries to substring(i,40) it gave error. As Andrew said there must be a check before substring – Shino Lex Apr 04 '19 at 10:44

1 Answers1

0

If inputString is "abc 201 lmn road pqr near xyz building state maharashtra navi mumbai, 4212001 .... "

The result of your extended function is:

private string[] splitToNChar(string inputString, int chunkSize)
{
    if (chunkSize > inputString.Length)
        throw new Exception("input string (" + inputString.Length + ") is smaller than chunksize (" + chunkSize + ")");

    List<string> myList = new List<string>();
    for (int i = 0; i < inputString.Length; i += chunkSize)
    {
        if (i + chunkSize < inputString.Length)
            myList.Add(inputString.Substring(i, chunkSize));
        else
            myList.Add(inputString.Substring(i)); //ADD REST OF STRING
    }
    return myList.ToArray();
}

Output:

[0]  abc 201 lmn road pqr near
[1]   xyz building state mahar
[2]  ashtra navi mumbai, 42120
[3]  01 ....

I hope I understand your question correctly.

Mar Tin
  • 2,132
  • 1
  • 26
  • 46
  • what to do if there is one word like in output 'maharashtra' it gets separated like 'mahar' & 'ashtra'. for such condition there should not be splitting it shoud include whole word there. – TejpalBh Apr 04 '19 at 11:17
  • You could split after a defined amount of words. It's depends on your data model, maybe you can share it, and I will take a look. – Mar Tin Apr 04 '19 at 11:33