I have a StringBuilder
instance where I am doing numerous sb.AppendLine("test");
for example.
How do I work out how many lines I have?
I see the class has .Length
but that tells me how many characters in all.
Any ideas?
We can also count the number of lines present in the given string using the IndexOf() method. This method is used to find the zero-based index of the first occurrence of the given character in the given string.
The length() method of StringBuilder class returns the number of character the StringBuilder object contains. The length of the sequence of characters currently represented by this StringBuilder object is returned by this method.
You can use . ToString() to get the String from the StringBuilder .
The toString() method of the StringBuilder class is the inbuilt method used to return a string representing the data contained by StringBuilder Object. A new String object is created and initialized to get the character sequence from this StringBuilder object and then String is returned by toString().
Sorted by efficiency:
The last one is extraordinary expensive and generates lots of garbage, don't use.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With