C# line break every n characters

后端 未结 5 931
孤街浪徒
孤街浪徒 2020-12-16 20:39

Suppose I have a string with the text: \"THIS IS A TEST\". How would I split it every n characters? So if n was 10, then it would display:

\"THIS IS A \"
\"T         


        
5条回答
  •  误落风尘
    2020-12-16 21:38

    You should be able to use a regex for this. Here is an example:

    //in this case n = 10 - adjust as needed
    List groups = (from Match m in Regex.Matches(str, ".{1,10}") 
                           select m.Value).ToList();
    
    string newString = String.Join(Environment.NewLine, lst.ToArray());
    

    Refer to this question for details:
    Splitting a string into chunks of a certain size

提交回复
热议问题