Loop over string chars. A loop iterates over individual string characters. It allows processing for each char. The foreach-loop and the for-loop are available for this purpose. The string indexer returns a char value.

Performance. For performance, it is sometimes easier for the JIT compiler to optimize the loops if you do not hoist the Length check outside of the for-loop. It is not often helpful to store the length in a local variable.

Regex. Sometimes using a loop is far more efficient than a Regex. Another advantage of loops is the ability to test each character and alert the user to special characters. Loops are imperative and offer more control.

However: Regular expressions offer more power in fewer characters. They are sometimes a better option.

Summary. This simple method loops over each character in a string. The performance here is good because it only needs to make one pass over the string. Often, we use loops to test for special patterns.

Tip: You can find a benchmark of different ways to loop over strings with the for-loop in the C# language on this site.