logo search
Учебник_ПОА

Using StringBuilder

The StringBuilder class creates a string buffer that offers better performance if your program performs a lot of string manipulation. The StringBuilder class also allows you to reassign individual characters, something the built-in string data type does not support.

In this example, a StringBuilder object is created, and its contents are added one by one using the Append method.

class TestStringBuilder

{

static void Main()

{

System.Text.StringBuilder sb = new System.Text.StringBuilder();

// Create a string composed of numbers 0 - 9

for (int i = 0; i < 10; i++)

{

sb.Append(i.ToString());

}

System.Console.WriteLine(sb); // displays 0123456789

// Copy one character of the string (not possible with a System.String)

sb[0] = sb[9];

System.Console.WriteLine(sb); // displays 9123456789

}

}