How to use StringBuilder in C#?


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#.

Updated on: 23-Jun-2020

218 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements