Split String into smaller Strings by length variable

前端 未结 12 1939
谎友^
谎友^ 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:54

        private string[] SplitByLength(string s, int d)
        {
            List stringList = new List();
            if (s.Length <= d) stringList.Add(s);
            else
            {
                int x = 0;
                for (; (x + d) < s.Length; x += d)
                {
                    stringList.Add(s.Substring(x, d));
                }
                stringList.Add(s.Substring(x));
            }
            return stringList.ToArray();
        }
    

提交回复
热议问题