3/21/2007 1:52:00 AM
Today I was faced with the need to determine the number of lines within a StringBuilder instance. If I had needed the length of the string contained within the StringBuilder, that would have been a trivial task.
After perusing MSDN, I was unable to find a property or method of the StringBuilder class that provided the needed behaviour. You would think that this would be accessible via something like StringBuilder.Lines.Count, but no luck.
So, I figured out a hack of sorts. If you first create a text box control (within code), you can determine the line count from the control.
private int GetLineCount(StringBuilder sb)
TextBox ctl = new TextBox();
ctl.Text = sb.ToString();
int lineCount = 0;
foreach (String s in ctl.Lines)
Voila! The number of lines within a StringBuilder. If anyone happens to know the 'correct' way of doing this from within the framework, please let me know.