#1,116 – Iterating Through a String Using the foreach Statement

You can iterate through the individual characters in a string using the foreach statement.  The iteration variable is of type char and is set consecutively to each character within the string as the foreach statement executes.

            string secret = "Kint is Keyser Soze";

            StringBuilder sbObfuscate = new StringBuilder();

            foreach (char c in secret)
                sbObfuscate.Append((char)(c + 3));