Creating strings in C#

By Aug 07, 2015


Creating strings in CSharp


In this article, we will create large strings of character by using the StringBuilder class.

In the article Basic string operations in C#, we give a multitude of ways to create strings from other strings and arrays, or using methods of various classes, but we will focus on the generation of long strings of characters.

Prefer StringBuilder for string concatenation

To add multiple strings in C# to a string of characters s, often used instructions of the type:

s = s + "string1";

s = s + "string2";

But if you need to add several thousands of strings, it becomes very slow.

For each statement of the type s = s + "string1" a new string is created and replaces the former which is destroyed by the garbage collector.

Consider using the StringBuilder class which allows to build a string by placing the characters in a buffer.

To get a string, just convert the StringBuilder object to a string.

Give a simple example:

       private void button_Click(object sender, RoutedEventArgs e)
           var watch = Stopwatch.StartNew();
           string s = "abcde";
           for (int i=0; i < 1000; i++)
               s += "0123456789";
           var elapsedMs = watch.ElapsedMilliseconds;
           textBlock.Text = elapsedMs.ToString();
           watch = Stopwatch.StartNew();
           StringBuilder sb = new StringBuilder("abcde");
           for (int i = 0; i < 1000; i++)
           s = sb.ToString();
           elapsedMs = watch.ElapsedMilliseconds;
           textBlock.Text += " ; " + watch.ElapsedMilliseconds;

After execution, it displays 2ms for the first and 0ms with StringBuilder.

For 10000 iterations, 147ms; 0ms

For 100000 iterations, 43590ms; 2ms

Generating large strings

This StringBuilder class is recommended for concatenating very large strings of characters.

What other operations can be performed on objects of class StringBuilder?

It is possible to replace all instances of one substring by another, remove a certain number of characters from a given position, copy a portion of the string into an array of characters, etc.

You can also compare to object of the class StringBuilder.

But for more complex manipulations, convert the StringBuilder object to array of characters or strings.

Write a program that randomly generates a StringBuilder string and that reverses the order of the characters.

       private void button_Click(object sender, RoutedEventArgs e)
           StringBuilder sb = new StringBuilder();
           Random r = new Random();
           char c = ' ';
           for (int i = 0; i < 10000; i++)
               c= (char)(r.Next(0, 10)+48);
           textBlock.Text = sb.ToString();
           int n = sb.Length;
           int n2 = sb.Length / 2;
           char[] ac = new char[n];
           sb.CopyTo(0, ac, 0, n);
           for (int i = 0; i < n2; i++)
               c = ac[i];
               ac[n - i - 1] = c;
           textBlock.Text += ";"+ new string(ac);