Determine the Number of Lines Within a StringBuilder

by Randall 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)
    {
       lineCount++;
    }

    return lineCount;

}

...

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.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

Related posts

Comments

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

10/6/2008 5:52:37 PM

Powered by BlogEngine.NET 1.3.1.0
Theme by Mads Kristensen

About the author

Name of author Randall Sexton
Currently a .Net developer for Bechtel Corporation in Oak Ridge, TN.

E-mail me Send mail

Calendar

<<  October 2008  >>
MoTuWeThFrSaSu
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

View posts in large calendar

Recent comments

Don't show

Authors

Categories


Logo Credit

My logo was taken from CodingHorror.
Jeff Atwood © Copyright 2007

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Sign in