Getting a char at specific index and enumerating the string

suggest change

You can use the Substring method to get any number of characters from a string at any given location. However, if you only want a single character, you can use the string indexer to get a single character at any given index like you do with an array:

string s = "hello";
char c = s[1]; //Returns 'e'

Notice that the return type is char, unlike the Substring method which returns a string type.

You can also use the indexer to iterate through the characters of the string:

string s = "hello";
foreach (char c in s)
    Console.WriteLine(c);
/********* This will print each character on a new line:
h
e
l
l
o
**********/

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Common String Operations:
* Getting a char at specific index and enumerating the string

Table Of Contents
10 Common String Operations
17 Regex
19 Arrays
21 Enum
22 Tuples
24 GUID
27 Looping
36 Casting
46 Methods
88 Events
92 Structs
104 Indexer
106 Stream
107 Timers
109 Threading
127 Caching
135 Pointers
147 C# Script