Export (0) Print
Expand All
Expand Minimize

JS1255: The plus operator is a slow way to concatenate strings

The plus operator is a slow way to concatenate strings. Consider using System.Text.StringBuilder instead.

The plus (+) operator concatenates strings. In many circumstances, such as appending many small strings to another string, System.Text.StringBuilder produces code that runs much faster.

For example, consider the following code that builds the string "0123456789". It generates this warning when compiled.

var a : String = "";
for(var i=0; i<10; i++)
   a += i;
print(a);

When run, this displays the string "0123456789".

When the previous example uses System.Text.StringBuilder, the program runs faster and does not generate the warning.

var b : System.Text.StringBuilder;
b = new System.Text.StringBuilder();
for(var i=0; i<10; i++)
   b.Append(i);
print(b);

Like the program before, this also displays "0123456789".

Another way to prevent the warning from being displayed is to use an untyped variable to hold the string to which other strings are appended.

To correct this error

  1. Use System.Text.StringBuilder for the type of the string to which other strings are appended, and rewrite the statements with the += operations to use the Append method instead.

  2. Use an untyped variable for the string to which other strings are appended. (This solution will not make the code run faster, but it will suppress the warning.)

Community Additions

ADD
Show:
© 2014 Microsoft