Split string in 512 char chunks

后端 未结 8 1668
我在风中等你
我在风中等你 2021-02-05 23:47

Maybe a basic question but let us say I have a string that is 2000 characters long, I need to split this string into max 512 character chunks each.

Is there a nice way,

8条回答
  •  盖世英雄少女心
    2021-02-06 00:27

    Though this question meanwhile has an accepted answer, here's a short version with the help of regular expressions. Purists may not like it (understandably) but when you need a quick solution and you are handy with regexes, this can be it. Performance is rather good, surprisingly:

    string [] split = Regex.Split(yourString, @"(?<=\G.{512})");
    

    What it does? Negative look-backward and remembering the last position with \G. It will also catch the last bit, even if it isn't dividable by 512.

提交回复
热议问题