C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C# program that uses foreach on string
using System;
class Program
{
static void Main()
{
const string s = "peace";
foreach (char c in s)
{
Console.WriteLine(c);
}
for (int i = 0; i < s.Length; i++)
{
Console.WriteLine(s[i]);
}
}
}
Output
p
e
a
c
e
p
e
a
c
e
However: Regular expressions offer more power in fewer characters. They are sometimes a better option.
Regex vs. LoopRegex