How to use StringBuilder in C#?

CsharpProgrammingServer Side Programming

With StringBuilder, you can expand the number of characters in the string. String cannot be changed once it is created, but StringBuildercan be expanded. It does not create a new object in the memory.

Initialize StringBuilder.

StringBuilder str = new StringBuilder();

Let us see an example to learn how to work with StringBuilder in C#.

Example

 Live Demo

using System;
using System.Text;
public class Program {
   public static void Main() {
      StringBuilder str = new StringBuilder("Web World!!",30);
      str.Replace("World", "Arena");
      Console.WriteLine(str);
   }
}

Output

Web Arena!!

Above the Replace() method of StringBuilder is used to to replace a string in C#.

raja
Published on 18-Sep-2018 12:44:07
Advertisements