StringBuilder.Chars[] Property in C#


The StringBuilder.Chars[] property gets or sets the character at the specified character position in this instance.

Syntax

The syntax is as follows -

public char this[int index] { get; set; }

Above, the index parameter is the position of the character.

Example

Let us now see an example -

 Live Demo

using System;
using System.Text;
public class Demo {
   public static void Main() {
      StringBuilder strBuilder = new StringBuilder("ghgh78hkjj");
      int num = 0;
      for (int i = 0; i < strBuilder.Length; i++) {
         char c = strBuilder[i];
         if (Char.IsDigit(c))
         num++;
      }
      Console.WriteLine("String = "+strBuilder);
      Console.WriteLine("Numbers in the string = "+num);
   }
}

Output

This will produce the following output -

String = ghgh78hkjj
Numbers in the string = 2

Example

Let us now see another example -

 Live Demo

using System;
using System.Text;
public class Demo {
   public static void Main() {
      StringBuilder strBuilder = new StringBuilder("ghgh78hkjj");
      char c = strBuilder[3];
      Console.WriteLine("String = "+strBuilder);
      Console.WriteLine("Character = "+c);
   }
}

Output

This will produce the following output -

String = ghgh78hkjj
Character = h

Updated on: 03-Dec-2019

61 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements