Join Multiple Strings With Single String
Join the DZone community and get the full member experience.Join For Free
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);Eclipse and Eclipse IDE tips.
Opinions expressed by DZone contributors are their own.