StringBuilder ToString() performance

c#, optimization, performance, stringbuilder, wpf

Solution

If you're looking for a `Substring` alternative that doesn't extract the entire string from the `StringBuilder` you could use the overload of `ToString` that takes a `start` and `length`:

string header;
foreach(StringBuilder strBuilder in bigArray)
{
    header= strBuilder.ToString(start, length);
}

Is it faster? I don't know - you'll have to measure it to know for sure.

Another alternative would be to store the "lines" as a collection of string arrays (or `List<List<string>>`) rather than a collection of `StringBuilder`s.

Problem

I need to extract the first line of many huge StringBuilders(around 1mb of data each) in a loop. To do so, I thought about doing this: ``` string header; foreach(StringBuilder strBuilder in bigArray) header= strBuilder.ToString().SubString(blabla); ``` but I'm worried the .ToString() method might take too long to execute. Do you have any idea about how I can do this efficiently?

Original source