StringBuilder.Chars[Int32] Property is used to get or set the character at the specified character position in this instance.
Syntax: public char this[int index] { get; set; }
Here, the index is the position of the character.Property Value: This property returns the Unicode character at position index.
Exceptions:
- ArgumentOutOfRangeException: If the index is outside the bounds of this instance while setting a character.
- IndexOutOfRangeException: If the index is outside the bounds of this instance while getting a character.
Below programs illustrate the use of the above-discussed property:
Example 1:
// C# program demonstrate // the Chars[Int32] Property using System; using System.Text; class GFG { // Main Method public static void Main(String[] args) { // create a StringBuilder object // with a String pass as parameter StringBuilder str = new StringBuilder("GeeksforGeeks"); // print string Console.WriteLine("String is " + str.ToString()); // loop through string // and print every Character for (int i = 0; i < str.Length; i++) { // get char at position i char ch = str[i]; // print char Console.WriteLine("Char at position " + i + " is " + ch); } } } |
String is GeeksforGeeks Char at position 0 is G Char at position 1 is e Char at position 2 is e Char at position 3 is k Char at position 4 is s Char at position 5 is f Char at position 6 is o Char at position 7 is r Char at position 8 is G Char at position 9 is e Char at position 10 is e Char at position 11 is k Char at position 12 is s
Example 2:
// C# program demonstrate // the Chars[Int32] Property using System; using System.Text; class GFG { // Main Method public static void Main(String[] args) { // create a StringBuilder object StringBuilder str = new StringBuilder(); // add the String to StringBuilder Object str.Append("Geek"); // get char at position 1 char ch = str[1]; // print the result Console.WriteLine("StringBuilder Object" + " contains = " + str); Console.WriteLine("Character at Position 1" + " in StringBuilder = " + ch); } } |
StringBuilder Object contains = Geek Character at Position 1 in StringBuilder = e
Reference:
Recommended Posts:
- Uri.IsLoopback Property in C# with Example
- Console.KeyAvailable() Property in C#
- Stack.Count Property in C#
- Stack.IsSynchronized Property in C#
- Queue.IsSynchronized Property in C#
- Queue.Count Property in C#
- How to use AutoCheck property of CheckBox in C#?
- How to use AcceptsReturn Property of TextBox in C#?
- C# | Dictionary.Item[] Property
- C# | Dictionary.Values Property
- C# | Dictionary.Keys Property
- C# | Dictionary.Count Property
- C# | LongLength property of an Array
- Uri.DnsSafeHost Property in C# with Examples
- Uri.Fragment Property in C# with Examples
- Thread.CurrentThread Property in C#
- C# | SortedDictionary.Keys Property
- C# | SortedDictionary.Values Property
- C# | SortedDictionary.Count Property
- C# | SortedDictionary.Item[] Property
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

