Append to StringBuilder in C#


The Append() method add content to a StringBuilder.

Set a String −

StringBuilder str = new StringBuilder();

Now, loop through the number of elements you want and use Append () to append to StringBuilder −

for (int j = 0; j < 5; j++) {
   str.Append(j).Append(" ");
}

The following is the complete code −

Example

 Live Demo

using System;
using System.Text;

class Program {
   static void Main() {
      StringBuilder str = new StringBuilder();

      for (int j = 0; j < 5; j++) {
         str.Append(j).Append(" ");
      }
      Console.WriteLine(str);
   }
}

Output

0 1 2 3 4

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 22-Jun-2020

383 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements