Split String into smaller Strings by length variable

前端 未结 12 1926
谎友^
谎友^ 2020-12-01 07:19

I\'d like to break apart a String by a certain length variable.
It needs to bounds check so as not explode when the last section of string is not as long as or longer th

12条回答
  •  粉色の甜心
    2020-12-01 07:55

    Easy to understand version:

    string x = "AAABBBCC";
    List a = new List();
    for (int i = 0; i < x.Length; i += 3)
    {
        if((i + 3) < x.Length)
            a.Add(x.Substring(i, 3));
        else
            a.Add(x.Substring(i));
    }
    

    Though preferably the 3 should be a nice const.

提交回复
热议问题