Flip the text to the next line if it exceeds a certain length?

I need to write different paragraphs of text in a specific area. For example, I drew a console window that looks like this:

/----------------------\ | | | | | | | | \----------------------/ 

How do I write text inside it, but wrap it on the next line if it's too long?

+8
c # console-application
source share
4 answers

Divide the last space to the length of the string?

 int myLimit = 10; string sentence = "this is a long sentence that needs splitting to fit"; string[] words = sentence.Split(new char[] { ' ' }); IList<string> sentenceParts = new List<string>(); sentenceParts.Add(string.Empty); int partCounter = 0; foreach (string word in words) { if ((sentenceParts[partCounter] + word).Length > myLimit) { partCounter++; sentenceParts.Add(string.Empty); } sentenceParts[partCounter] += word + " "; } foreach (string x in sentenceParts) Console.WriteLine(x); 

UPDATE (the above solution has lost the last word in some cases):

 int myLimit = 10; string sentence = "this is a long sentence that needs splitting to fit"; string[] words = sentence.Split(' '); StringBuilder newSentence = new StringBuilder(); string line = ""; foreach (string word in words) { if ((line + word).Length > myLimit) { newSentence.AppendLine(line); line = ""; } line += string.Format("{0} ", word); } if (line.Length > 0) newSentence.AppendLine(line); Console.WriteLine(newSentence.ToString()); 
+10
source share

I modified the version of Jim H to support some special cases. For example, the case when the sentence does not contain a white space; I also noted that there is a problem when the line has a space in the last position; then space will be added at the end and you will get too many characters.

Here is my version just in case anyone is interested:

 public static List<string> WordWrap(string input, int maxCharacters) { List<string> lines = new List<string>(); if (!input.Contains(" ")) { int start = 0; while (start < input.Length) { lines.Add(input.Substring(start, Math.Min(maxCharacters, input.Length - start))); start += maxCharacters; } } else { string[] words = input.Split(' '); string line = ""; foreach (string word in words) { if ((line + word).Length > maxCharacters) { lines.Add(line.Trim()); line = ""; } line += string.Format("{0} ", word); } if (line.Length > 0) { lines.Add(line.Trim()); } } return lines; } 
+2
source share

I changed the version of Manfred. If you put a line with the character "\ n" in it, it will wrap the text strangely, because it will consider it as another character. With this minor change, everything will go smoothly.

 public static List<string> WordWrap(string input, int maxCharacters) { List<string> lines = new List<string>(); if (!input.Contains(" ") && !input.Contains("\n")) { int start = 0; while (start < input.Length) { lines.Add(input.Substring(start, Math.Min(maxCharacters, input.Length - start))); start += maxCharacters; } } else { string[] paragraphs = input.Split('\n'); foreach (string paragraph in paragraphs) { string[] words = paragraph.Split(' '); string line = ""; foreach (string word in words) { if ((line + word).Length > maxCharacters) { lines.Add(line.Trim()); line = ""; } line += string.Format("{0} ", word); } if (line.Length > 0) { lines.Add(line.Trim()); } } } return lines; } 
+2
source share

I started with Jim H. solution and ended up with this method. Only problem is that the text has any word that exceeds the limit. But it works well.

 public static List<string> GetWordGroups(string text, int limit) { var words = text.Split(new string[] { " ", "\r\n", "\n" }, StringSplitOptions.None); List<string> wordList = new List<string>(); string line = ""; foreach (string word in words) { if (!string.IsNullOrWhiteSpace(word)) { var newLine = string.Join(" ", line, word).Trim(); if (newLine.Length >= limit) { wordList.Add(line); line = word; } else { line = newLine; } } } if (line.Length > 0) wordList.Add(line); return wordList; } 
+1
source share

All Articles