Two interesting things about C# strings
1. Iterating with foreach
The String class implements the IEnumerable interface, so you can iterate over it using a foreach statement. Each iteration will yield subsequent characters from the string:
string myString = "I'm out of the loop, and that's the way I like it";
foreach (char ch in myString)
{
Console.WriteLine(ch);
}
2. The indexer
The String class provides an indexer, it returns a Char object that corresponds to the specified position of the string, for example:
string myString = "But it is, perhaps, the end of the beginning";
char first = myString[0]; // 'B'
char last = myString[myString.Length - 1]; // 'g'
Thursday, 02 November 2006
blog comments powered by Disqus