Split text into lines with maximum length

I have a long string, and I want to put it in a small field. To achieve this, I break the line into lines by spaces. The algorithm is as follows:

public static string BreakLine(string text, int maxCharsInLine) { int charsInLine = 0; StringBuilder builder = new StringBuilder(); for (int i = 0; i < text.Length; i++) { char c = text[i]; builder.Append(c); charsInLine++; if (charsInLine >= maxCharsInLine && char.IsWhiteSpace(c)) { builder.AppendLine(); charsInLine = 0; } } return builder.ToString(); } 

But it breaks when there is a short word, followed by a longer word. "foo howcomputerwork" with a maximum length of 16 will not break, but I want this. I thought I had the prospect of seeing where the next gap occurs, but I'm not sure if this will lead to the fewest possible lines.

+7
source share
3 answers

Enjoy it!

 public static string SplitToLines(string text, char[] splitOnCharacters, int maxStringLength) { var sb = new StringBuilder(); var index = 0; while (text.Length > index) { // start a new line, unless we've just started if (index != 0) sb.AppendLine(); // get the next substring, else the rest of the string if remainder is shorter than `maxStringLength` var splitAt = index + maxStringLength <= text.Length ? text.Substring(index, maxStringLength).LastIndexOfAny(splitOnCharacters) : text.Length - index; // if can't find split location, take `maxStringLength` characters splitAt = (splitAt == -1) ? maxStringLength : splitAt; // add result to collection & increment index sb.Append(text.Substring(index, splitAt).Trim()); index += splitAt; } return sb.ToString(); } 

Note that splitOnCharacters and maxStringLength can be saved in the application settings area.

+6
source

Check the contents of the character before writing to the line builder and or with the current counter:

  public static string BreakLine(string text, int maxCharsInLine) { int charsInLine = 0; StringBuilder builder = new StringBuilder(); for (int i = 0; i < text.Length; i++) { char c = text[i]; if (char.IsWhiteSpace(c) || charsInLine >= maxCharsInLine) { builder.AppendLine(); charsInLine = 0; } else { builder.Append(c); charsInLine++; } } return builder.ToString(); } 
+1
source

update the code a bit, @ dead.rabit will go into the loop someday.

  public static string SplitToLines(string text,char[] splitanyOf, int maxStringLength) { var sb = new System.Text.StringBuilder(); var index = 0; var loop = 0; while (text.Length > index) { // start a new line, unless we've just started if (loop != 0) { sb.AppendLine(); } // get the next substring, else the rest of the string if remainder is shorter than `maxStringLength` var splitAt = 0; if (index + maxStringLength <= text.Length) { splitAt = text.Substring(index, maxStringLength).LastIndexOfAny(splitanyOf); } else { splitAt = text.Length - index; } // if can't find split location, take `maxStringLength` characters if (splitAt == -1 || splitAt == 0) { splitAt = text.IndexOfAny(splitanyOf, maxStringLength); } // add result to collection & increment index sb.Append(text.Substring(index, splitAt).Trim()); if(text.Length > splitAt) { text = text.Substring(splitAt + 1).Trim(); } else { text = string.Empty; } loop = loop + 1; } return sb.ToString(); } 
0
source

All Articles