Stringbuilder error

S

Soulless

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?
 
P

Peter Duniho

Is there a size limit on the size of my stringbuilder object?

No. The problem is that you are letting your counter equal
tempArray.Length, when in fact it should only be used when it's less
than tempArray.Length.

Pete
 
G

Guest

Hi Soulless,

The exception happens here:
tempArray[counter]

when the value of the counter reaches tempArray.Length.

Note that if you have an array of a given length, e.g. 6, its last element's
index will be 5.

Therefore, you should rewrite your for loop like this:
for (int counter = 0; counter < tempArray.Length; counter++)


Hope this helps,
 
J

Jon Skeet [C# MVP]

Soulless said:
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.)
 
G

Guest

Would StringBuilder.AppendLine() have any disadvantages that one should be
aware?

Jon Skeet said:
Soulless said:
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.)
 
J

Jon Skeet [C# MVP]

Family Tree Mike said:
Would StringBuilder.AppendLine() have any disadvantages that one should be
aware?

No - indeed, that's a really good option that I unfortunately forgot :(

(I'd still use StreamWriter directly in preference though.)
 
M

Maximiliano

Would StringBuilder.AppendLine() have any disadvantages that one should be
aware?

Jon Skeet [C# MVP]" wrote:> Soulless said:
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.)

Hi I think the problem is in the for condicion condicion.

for (int counter = 0; counter <= tempArray.Length; counter++)

Should be

for (int counter = 0; counter < tempArray.Length; counter++)

only "minus" without equal, becouse for example if the array has 5
elements the array begins in "0" position. In the example from 0 to 4
= 5 elements (position 5 does not exist).
Bye.
 
J

Jon Skeet [C# MVP]

Hi I think the problem is in the for condicion condicion.

for (int counter = 0; counter <= tempArray.Length; counter++)

Should be

for (int counter = 0; counter < tempArray.Length; counter++)

Absolutely - as I said, others had pointed out the reason the original
code was going "bang" - I was proposing alternative code which would
have avoided the problem in the first place.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top