Access a Character in C# StringBuilder



Firstly, set the StringBuilder −

StringBuilder str = new StringBuilder();
str.Append("premium");

To access the 5th character and display it −

Console.WriteLine(str[4]);

The following is the complete code −

Example

 Live Demo

using System;
using System.Text;

public class Demo {
   public static void Main() {
      StringBuilder str = new StringBuilder();
      str.Append("premium");
      Console.WriteLine("String : "+str);
      Console.Write("Accessing 5th character : ");
      Console.WriteLine(str[4]);
   }
}

Output

String : premium
Accessing 5th character : i
Updated on: 2020-06-22T13:35:57+05:30

384 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements