Access a character in C# StringBuilder

A StringBuilder in C# allows you to access individual characters using the indexer syntax, similar to accessing elements in an array. The indexer provides both read and write access to characters at specific positions within the StringBuilder.

Syntax

Following is the syntax for accessing a character in StringBuilder −

char character = stringBuilder[index];

Following is the syntax for modifying a character in StringBuilder −

stringBuilder[index] = 'newCharacter';

Parameters

  • index − The zero-based position of the character to access or modify.

Return Value

Returns the character at the specified index position when used for reading.

Using StringBuilder Indexer for Reading Characters

You can access any character in a StringBuilder by specifying its zero-based index position −

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]);
        Console.Write("First character : ");
        Console.WriteLine(str[0]);
        Console.Write("Last character : ");
        Console.WriteLine(str[str.Length - 1]);
    }
}

The output of the above code is −

String : premium
Accessing 5th character : i
First character : p
Last character : m

Using StringBuilder Indexer for Modifying Characters

The StringBuilder indexer also allows you to modify individual characters at specific positions −

using System;
using System.Text;

public class Demo {
    public static void Main() {
        StringBuilder str = new StringBuilder("hello");
        Console.WriteLine("Original string: " + str);
        
        // Modify first character
        str[0] = 'H';
        Console.WriteLine("After modifying first character: " + str);
        
        // Modify last character
        str[str.Length - 1] = '!';
        Console.WriteLine("After modifying last character: " + str);
        
        // Modify middle character
        str[2] = 'L';
        Console.WriteLine("After modifying middle character: " + str);
    }
}

The output of the above code is −

Original string: hello
After modifying first character: Hello
After modifying last character: Hell!
After modifying middle character: HeL!

Index Bounds Checking

Attempting to access an index outside the valid range will throw an IndexOutOfRangeException

using System;
using System.Text;

public class Demo {
    public static void Main() {
        StringBuilder str = new StringBuilder("test");
        Console.WriteLine("StringBuilder length: " + str.Length);
        
        try {
            Console.WriteLine("Valid access - index 2: " + str[2]);
            Console.WriteLine("Invalid access - index 10: " + str[10]);
        }
        catch (IndexOutOfRangeException ex) {
            Console.WriteLine("Exception: " + ex.Message);
        }
    }
}

The output of the above code is −

StringBuilder length: 4
Valid access - index 2: s
Exception: Index was outside the bounds of the array.

Conclusion

StringBuilder indexer provides convenient access to individual characters using zero-based indexing. It supports both reading and writing operations, making it easy to manipulate specific characters within the StringBuilder without converting to a string first.

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

485 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements