Stringbuilder.Chars[] Property In C#

C# StringBuilder is similar to Java StringBuilder in functionality. Unlike String objects, which are immutable, StringBuilder allows for modifying strings after creation. When using functions from the System.String class, a new string object is generated each time. For instance, changing the string "NetworkProgramming" to "NP" will create a new string object in memory instead of updating the original string at its existing memory location.

The StringBuilder class is essential for making frequent changes to a string. In C#, the StringBuilder concept is introduced to avoid modifying, appending, removing, or inserting new strings into the original string. StringBuilder is a dynamically generated object that expands the allocated memory as needed for the updated or added string, instead of creating a new object in memory.

The specific character within a string can be accessed or modified by utilizing the StringBuilder.Chars[Int32] Property.

Syntax:

It has the following syntax:

Example

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

In this particular syntax, the "in" pertains to the position of the character. This attribute provides the Unicode characters located at the designated index.

Exceptions:

  • ArgumentOutOfRangeException: When setting a character and the index is outside this instance's constraints.
  • IndexOutOfRangeException: If the index is not inside the bounds of this instance while retrieving a character.
  • Example 1:

Let's consider a scenario to demonstrate the Char[Int] technique in C#.

Example

using System; 
using System.Text; 
class CharArray{ 

	// Main Method 
	public static void Main(String[] args) 
	{ 
		// A string builder object with the string as a parameter is passed
		StringBuilder st = new StringBuilder("ProgrammingLanguage"); 
		// statement to display the string
		Console.WriteLine("The input String is"+ st.ToString()); 
		// loop through the string to print every character
		for (int i = 0; i < st.Length; i++) { 
			// char at each position
			char c = st[i]; 
			// print char 
			Console.WriteLine("The Character at  position"+ i + "is" +c);
		} 
	} 
}

Output:

Output

The input String is Programming Language
The Character at position 0 is P
The Character at position 1 is r
The Character at position 2 is o
The Character at position 3 is g
The Character at position 4 is r
The Character at position 5 is a
The Character at position 6 is m
The Character at position 7 is m
The Character at position 8 is i
The Character at position 9 is n
The Character at position 10 is g
The Character at position 11 is L
The Character at position 12 is a
The Character at position 13 is n
The Character at position 14 is g
The Character at position 15 is u
The Character at position 16 is a
The Character at position 17 is g
The Character at position 18 is e

Example 2:

Let's consider a scenario to demonstrate the Char[Int] function in C#.

Example

using System; 
using System.Text; 
class CharArray{ 
	// Main Method
	public static void Main(String[] args) 
	{ 
		// create a StringBuilder input 
		StringBuilder st = new StringBuilder(); 
		// adding string to the string builder
		st.Append("Programming"); 
		// get char at position 1 
		char cha = st[2]; 
		// Console statement
		Console.WriteLine("StringBuilder Object" + " contains = " + st); 
        Console.WriteLine("The Character at the Position 2"+ "nd StringBuilder = " + cha); 
	} 
}

Output:

Output

StringBuilder Object contains = Programming
The Character at the Position 2nd StringBuilder = o

Input Required

This code uses input(). Please provide values below: