This post will discuss how to iterate through the characters of a string in C#.

1. Using foreach loop

The foreach loop provides a simple, elegant way to iterate through the characters of a string. The following example demonstrates how to use foreach to print every character of a string in the console.

Download  Run Code

Output:

H
e
l
l
o

2. Using for loop

We can also iterate over characters of a string using the regular for loop and use the loop counter index to access each character. This is demonstrated below. Note that this is typically faster than the foreach statement.

Download  Run Code

Output:

H
e
l
l
o

That’s all about iterating through characters of a string in C#.