C# line break every n characters

后端 未结 5 921
孤街浪徒
孤街浪徒 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:12

    Probably not the most optimal way, but without regex:

    string test = "my awesome line of text which will be split every n characters";
    int nInterval = 10;
    string res = String.Concat(test.Select((c, i) => i > 0 && (i % nInterval) == 0 ? c.ToString() + Environment.NewLine : c.ToString()));
    

提交回复
热议问题