|
Friday, 23 December 2011 00:10 |
There are two ways to join multiple strings: using the + operator that the String class overloads, and using the StringBuilder class. The + operator is easy to use and makes for intuitive code, but it works in series; a new string is created for each use of the operator, so chaining multiple operators together is inefficient.
Example: 1
string two = "two";
string str = "one " + two + " three";
System.Console.WriteLine(str);
Example: 2
string two = "two";
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("one ");
sb.Append(two);
sb.Append(" three");
System.Console.WriteLine(sb.ToString());
string str = sb.ToString();
System.Console.WriteLine(str);
 Read more: |