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


 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");


Web Arena!!

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

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