StringBuilder.Chars[] Property in C#

The StringBuilder.Chars[] property in C# provides indexed access to individual characters within a StringBuilder instance. This property allows you to both get and set characters at specific positions, making it useful for character-level manipulations without converting the entire StringBuilder to a string.

Syntax

Following is the syntax for the StringBuilder.Chars[] property −

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

Parameters

  • index − The zero-based position of the character to get or set. Must be within the bounds of the StringBuilder (0 to Length-1).

Return Value

Returns the character at the specified index position. When used as a setter, it modifies the character at that position.

Using StringBuilder.Chars[] to Get Characters

The following example demonstrates how to access individual characters and count digits in a StringBuilder −

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

The output of the above code is −

String = ghgh78hkjj
Numbers in the string = 2

Using StringBuilder.Chars[] to Access Specific Characters

This example shows how to retrieve a character at a specific index position −

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 at index 3 = " + c);
   }
}

The output of the above code is −

String = ghgh78hkjj
Character at index 3 = h

Using StringBuilder.Chars[] to Modify Characters

The Chars[] property can also be used to modify individual characters in place −

using System;
using System.Text;

public class Demo {
   public static void Main() {
      StringBuilder strBuilder = new StringBuilder("Hello World");
      Console.WriteLine("Original: " + strBuilder);
      
      // Replace characters at specific positions
      strBuilder[0] = 'h';  // lowercase 'H'
      strBuilder[6] = 'w';  // lowercase 'W'
      
      Console.WriteLine("Modified: " + strBuilder);
      
      // Replace all vowels with '*'
      string vowels = "aeiouAEIOU";
      for (int i = 0; i < strBuilder.Length; i++) {
         if (vowels.Contains(strBuilder[i])) {
            strBuilder[i] = '*';
         }
      }
      Console.WriteLine("Vowels replaced: " + strBuilder);
   }
}

The output of the above code is −

Original: Hello World
Modified: hello world
Vowels replaced: h*ll* w*rld

Conclusion

The StringBuilder.Chars[] property provides efficient character-level access to StringBuilder contents. It allows both reading and writing individual characters at specific positions, making it ideal for in-place string modifications without the overhead of creating new string objects.

Updated on: 2026-03-17T07:04:36+05:30

162 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements