Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharpNotesForProfessionals.pdf
Скачиваний:
57
Добавлен:
20.05.2023
Размер:
6.12 Mб
Скачать

Chapter 13: String Concatenate

Section 13.1: + Operator

string s1 = "string1"; string s2 = "string2";

string s3 = s1 + s2; // "string1string2"

Section 13.2: Concatenate strings using

System.Text.StringBuilder

Concatenating strings using a StringBuilder can o er performance advantages over simple string concatenation using +. This is due to the way memory is allocated. Strings are reallocated with each concatenation, StringBuilders allocate memory in blocks only reallocating when the current block is exhausted. This can make a huge di erence when doing a lot of small concatenations.

StringBuilder sb = new StringBuilder(); for (int i = 1; i <= 5; i++)

{

sb.Append(i); sb.Append(" ");

}

Console.WriteLine(sb.ToString()); // "1 2 3 4 5 "

Calls to Append() can be daisy chained, because it returns a reference to the StringBuilder:

StringBuilder sb = new StringBuilder(); sb.Append("some string ")

.Append("another string");

Section 13.3: Concat string array elements using String.Join

The String.Join method can be used to concatenate multiple elements from a string array.

string[] value = {"apple", "orange", "grape", "pear"}; string separator = ", ";

string result = String.Join(separator, value, 1, 2); Console.WriteLine(result);

Produces the following output: "orange, grape"

This example uses the String.Join(String, String[], Int32, Int32) overload, which specifies the start index and count on top of the separator and value.

If you do not wish to use the startIndex and count overloads, you can join all string given. Like this:

string[] value = {"apple", "orange", "grape", "pear"}; string separator = ", ";

string result = String.Join(separator, value); Console.WriteLine(result);

GoalKicker.com – C# Notes for Professionals

68

which will produce;

apple, orange, grape, pear

Section 13.4: Concatenation of two strings using $

$ provides an easy and a concise method to concatenate multiple strings.

var str1 = "text1"; var str2 = " "; var str3 = "text3";

string result2 = $"{str1}{str2}{str3}"; //"text1 text3"

GoalKicker.com – C# Notes for Professionals

69