Search strings
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Search strings.
using System;
public class StringSearchDemo {
public static void Main() {
string str = "C# has powerful string handling.";
int idx;
Console.WriteLine("str: " + str);
idx = str.IndexOf('h');
Console.WriteLine("Index of first 'h': " + idx);
idx = str.LastIndexOf('h');
Console.WriteLine("Index of last 'h': " + idx);
idx = str.IndexOf("ing");
Console.WriteLine("Index of first \"ing\": " + idx);
idx = str.LastIndexOf("ing");
Console.WriteLine("Index of last \"ing\": " + idx);
char[] chrs = { 'a', 'b', 'c' };
idx = str.IndexOfAny(chrs);
Console.WriteLine("Index of first 'a', 'b', or 'c': " + idx);
if(str.StartsWith("C# has"))
Console.WriteLine("str begins with \"C# has\"");
if(str.EndsWith("ling."))
Console.WriteLine("str ends with \"ling.\"");
}
}
Related examples in the same category