Splitting a string into chunks of a certain size

后端 未结 30 2046
时光说笑
时光说笑 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:13

    class StringHelper
    {
        static void Main(string[] args)
        {
            string str = "Hi my name is vikas bansal and my email id is bansal.vks@gmail.com";
            int offSet = 10;
    
            List chunks = chunkMyStr(str, offSet);
    
            Console.Read();
        }
    
        static List chunkMyStr(string str, int offSet)
        {
    
    
            List resultChunks = new List();
    
            for (int i = 0; i < str.Length; i += offSet)
            {
                string temp = str.Substring(i, (str.Length - i) > offSet ? offSet : (str.Length - i));
                Console.WriteLine(temp);
                resultChunks.Add(temp);
    
    
            }
    
            return resultChunks;
        }
    }
    

提交回复
热议问题