for each string in string c# code example

Example 1: loop through string array c#

string[] arr = new string[4]; // Initialize.
        arr[0] = "one";               // Element 1.
        arr[1] = "two";               // Element 2.
        arr[2] = "three";             // Element 3.
        arr[3] = "four";              // Element 4.

        // Loop over strings.
        foreach (string s in arr)
        {
            Console.WriteLine(s);
        }

Example 2: c# iterate over string

// Regular Strings
string foo = "hello world", bar = string.Empty;

foreach(char c in foo){
    bar += c;
}

// StringBuilder
string foo = "hello world";
StringBuilder bar = new StringBuilder();

foreach (char c in foo)
{
    bar.Append(c);
}