Splitting a string into chunks of a certain size

后端 未结 30 2007
时光说笑
时光说笑 2020-11-22 07:55

Suppose I had a string:

string str = \"1111222233334444\"; 

How can I break this string into chunks of some size?

e.g., breaking t

30条回答
  •  一整个雨季
    2020-11-22 08:18

    Best , Easiest and Generic Answer :).

        string originalString = "1111222233334444";
        List test = new List();
        int chunkSize = 4; // change 4 with the size of strings you want.
        for (int i = 0; i < originalString.Length; i = i + chunkSize)
        {
            if (originalString.Length - i >= chunkSize)
                test.Add(originalString.Substring(i, chunkSize));
            else
                test.Add(originalString.Substring(i,((originalString.Length - i))));
        }
    

提交回复
热议问题