Splitting a string into chunks of a certain size

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

    Using regular expressions and Linq:

    List groups = (from Match m in Regex.Matches(str, @"\d{4}")
                           select m.Value).ToList();
    

    I find this to be more readable, but it's just a personal opinion. It can also be a one-liner : ).

提交回复
热议问题