Phrase Wrapping in .NET.

Is there any method in .net that wraps phrases with the maximum length for each line?

Example:

Phrase: The quick red fox jumps over the lazy cat Length: 20 

Result:

 The quick red fox jumps over the lazy cat 
+4
source share
2 answers

There is no built-in method for this. You can use regex:

 string text = "The quick brown fox jumps over the lazy dog."; int minLength = 1; int maxLength = 20; MatchCollection lines = Regex.Matches(text, "(.{"+minLength.ToString()+","+maxLength.ToString()+"})(?: |$)|([^ ]{"+maxLength.ToString()+"})"); StringBuilder builder = new StringBuilder(); foreach (Match line in lines) builder.AppendLine(line.Value); text = builder.ToString(); 

Note: I fixed pangram .

+7
source

The code in this article returns a list of strings, but you should easily adapt it.

C # Wrapping text with split and List <>

http://bryan.reynoldslive.com/post/Wrapping-string-data.aspx

0
source

All Articles