Soulless <dg*******@gmail.comwrote:
I am trying to implement the stringbuilder class by taking a LARGE
multiline text box and looping through the lines and build a string
and include \r\n in it.
I get : Index was outside the bounds of the array.
The code is simply:
StringBuilder str = new StringBuilder();
for (int counter = 0; counter <= tempArray.Length; counter++)
{
str.Append(tempArray[counter] + @"\r\n");
}
Is there a size limit on the size of my stringbuilder object?
Others have said what was causing the problem. Personally, however, I'd
go about it differently:
StringBuilder builder = new StringBuilder();
foreach (string line in tempArray)
{
builder.Append(line);
builder.Append(@"\r\n");
}
That has two advantages:
1) Using foreach is more straightforward in both code and conveying
intention than using the array index
2) By using string concatenation to construct the argument of the call
to Append, you are creating an extra copy of a string for no good
reason. (An alternative to calling Append twice would be to use
AppendFormat.)
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog:
http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too