a.文字搜尋

下列範例會判斷熟悉引號的子字串是否包含"fox"的字串。 如果字串中找到"fox",它也會顯示其起始位置。

C#

複製

using System; class Example { public static void Main() { string s1 = "The quick brown fox jumps over the lazy dog"; string s2 = "fox"; bool b = s1.Contains(s2); Console.WriteLine("'{0}' is in the string '{1}': {2}", s2, s1, b); if (b) { int index = s1.IndexOf(s2); if (index >= 0) Console.WriteLine("'{0} begins at character position {1}", s2, index + 1); } } } // This example display the following output:// 'fox' is in the string 'The quick brown fox jumps over the lazy dog': True// 'fox begins at character position 17