信息
您所需的主题如下所示。但此主题未包含在此库中。

String.IndexOf 方法 (String, StringComparison)

报告指定的字符串在当前 String 对象中的第一个匹配项的从零开始的索引。 一个参数指定要用于指定字符串的搜索类型。

命名空间:  System
程序集:  mscorlib(在 mscorlib.dll 中)

public int IndexOf(
	string value,
	StringComparison comparisonType
)

参数

value
类型:System.String
要搜寻的字符串。
comparisonType
类型:System.StringComparison
指定搜索规则的枚举值之一。

返回值

类型:System.Int32
如果找到该字符串,则为 value 参数的索引位置;如果未找到该字符串,则为 -1。 如果 valueEmpty,则返回值为 0。

异常条件
ArgumentNullException

valuenull

ArgumentException

comparisonType 不是有效的 System.StringComparison 值。

索引编号从零开始。

comparisonType 参数指定使用当前区域性还是固定区域性、使用区分大小写的搜索还是不区分大小写的搜索、以及使用字比较规则还是序号比较规则来搜索 value 参数。

对调用者的说明

字符集包括可忽略字符,在执行语言性的或区分区域性的比较时该字符不被考虑。 在区分区域性的搜索 (即,如果 comparisonType 不是 StringComparison.OrdinalStringComparison.OrdinalIgnoreCase) 中,如果 value 包含一个可忽略字符,则结果与移除了该字符的搜索等效。 如果 value 仅包含一个或多个可忽略字符,IndexOf(String, StringComparison) 方法始终返回 0 (零)来表示在当前实例的开始处找到匹配。

在下面的示例中,IndexOf(String, StringComparison) 方法用于在两个字符串中查找三个子字符串 (虚拟连字符 (U+00AD),一个虚拟连字符后有“n”个字符另一个虚拟连字符后有“m”个字符) 。 只有一个字符串包含软连字符。 如果示例在 .NET Framework 4 或更高版本上运行,因为指针,连字符是一 ignorable 字符,区分区域性的搜索返回将返回相同的值,如果悬停连字符搜索字符串中不包含。 顺序搜索,成功找到在一个字符串的虚拟连字符并报告该字符串在第二个字符串中缺失。


using System;

public class Example
{
   public static void Main()
   {
      string s1 = "ani\u00ADmal";
      string s2 = "animal";

      Console.WriteLine("Culture-sensitive comparison:");
      // Use culture-sensitive comparison to find the soft hyphen.
      Console.WriteLine(s1.IndexOf("\u00AD", StringComparison.CurrentCulture));
      Console.WriteLine(s2.IndexOf("\u00AD", StringComparison.CurrentCulture));

      // Use culture-sensitive comparison to find the soft hyphen followed by "n".
      Console.WriteLine(s1.IndexOf("\u00ADn", StringComparison.CurrentCulture));
      Console.WriteLine(s2.IndexOf("\u00ADn", StringComparison.CurrentCulture));

      // Use culture-sensitive comparison to find the soft hyphen followed by "m".
      Console.WriteLine(s1.IndexOf("\u00ADm", StringComparison.CurrentCulture));
      Console.WriteLine(s2.IndexOf("\u00ADm", StringComparison.CurrentCulture));

      Console.WriteLine("Ordinal comparison:");
      // Use ordinal comparison to find the soft hyphen.
      Console.WriteLine(s1.IndexOf("\u00AD", StringComparison.Ordinal));
      Console.WriteLine(s2.IndexOf("\u00AD", StringComparison.Ordinal));

      // Use ordinal comparison to find the soft hyphen followed by "n".
      Console.WriteLine(s1.IndexOf("\u00ADn", StringComparison.Ordinal));
      Console.WriteLine(s2.IndexOf("\u00ADn", StringComparison.Ordinal));

      // Use ordinal comparison to find the soft hyphen followed by "m".
      Console.WriteLine(s1.IndexOf("\u00ADm", StringComparison.Ordinal));
      Console.WriteLine(s2.IndexOf("\u00ADm", StringComparison.Ordinal));
   }
}
// The example displays the following output:
//       Culture-sensitive comparison:
//       0
//       0
//       1
//       1
//       4
//       3
//       Ordinal comparison:
//       3
//       -1
//       -1
//       -1
//       3
//       -1


下面的示例演示 IndexOf 方法的三个重载,该方法使用 StringComparison 枚举的不同值在另一个字符串内查找某个字符串的第一个匹配项。


// This code example demonstrates the 
// System.String.IndexOf(String, ..., StringComparison) methods.

using System;
using System.Threading;
using System.Globalization;

class Sample 
{
    public static void Main() 
    {
    string intro = "Find the first occurrence of a character using different " + 
                   "values of StringComparison.";
    string resultFmt = "Comparison: {0,-28} Location: {1,3}";

// Define a string to search for.
// U+00c5 = LATIN CAPITAL LETTER A WITH RING ABOVE
    string CapitalAWithRing = "\u00c5"; 

// Define a string to search. 
// The result of combining the characters LATIN SMALL LETTER A and COMBINING 
// RING ABOVE (U+0061, U+030a) is linguistically equivalent to the character 
// LATIN SMALL LETTER A WITH RING ABOVE (U+00e5).
    string cat = "A Cheshire c" + "\u0061\u030a" + "t";

    int loc = 0;
    StringComparison[] scValues = {
        StringComparison.CurrentCulture,
        StringComparison.CurrentCultureIgnoreCase,
        StringComparison.InvariantCulture,
        StringComparison.InvariantCultureIgnoreCase,
        StringComparison.Ordinal,
        StringComparison.OrdinalIgnoreCase };

// Clear the screen and display an introduction.
    Console.Clear();
    Console.WriteLine(intro);

// Display the current culture because culture affects the result. For example, 
// try this code example with the "sv-SE" (Swedish-Sweden) culture.

    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
    Console.WriteLine("The current culture is \"{0}\" - {1}.", 
                       Thread.CurrentThread.CurrentCulture.Name,
                       Thread.CurrentThread.CurrentCulture.DisplayName);

// Display the string to search for and the string to search.
    Console.WriteLine("Search for the string \"{0}\" in the string \"{1}\"", 
                       CapitalAWithRing, cat);
    Console.WriteLine();

// Note that in each of the following searches, we look for 
// LATIN CAPITAL LETTER A WITH RING ABOVE in a string that contains 
// LATIN SMALL LETTER A WITH RING ABOVE. A result value of -1 indicates 
// the string was not found.
// Search using different values of StringComparison. Specify the start 
// index and count. 

    Console.WriteLine("Part 1: Start index and count are specified.");
    foreach (StringComparison sc in scValues)
        {
        loc = cat.IndexOf(CapitalAWithRing, 0, cat.Length, sc);
        Console.WriteLine(resultFmt, sc, loc);
        }

// Search using different values of StringComparison. Specify the 
// start index. 
    Console.WriteLine("\nPart 2: Start index is specified.");
    foreach (StringComparison sc in scValues)
        {
        loc = cat.IndexOf(CapitalAWithRing, 0, sc);
        Console.WriteLine(resultFmt, sc, loc);
        }

// Search using different values of StringComparison. 
    Console.WriteLine("\nPart 3: Neither start index nor count is specified.");
    foreach (StringComparison sc in scValues)
        {
        loc = cat.IndexOf(CapitalAWithRing, sc);
        Console.WriteLine(resultFmt, sc, loc);
        }
    }
}

/*
Note: This code example was executed on a console whose user interface 
culture is "en-US" (English-United States).

This code example produces the following results:

Find the first occurrence of a character using different values of StringComparison.
The current culture is "en-US" - English (United States).
Search for the string "Å" in the string "A Cheshire ca°t"

Part 1: Start index and count are specified.
Comparison: CurrentCulture               Location:  -1
Comparison: CurrentCultureIgnoreCase     Location:  12
Comparison: InvariantCulture             Location:  -1
Comparison: InvariantCultureIgnoreCase   Location:  12
Comparison: Ordinal                      Location:  -1
Comparison: OrdinalIgnoreCase            Location:  -1

Part 2: Start index is specified.
Comparison: CurrentCulture               Location:  -1
Comparison: CurrentCultureIgnoreCase     Location:  12
Comparison: InvariantCulture             Location:  -1
Comparison: InvariantCultureIgnoreCase   Location:  12
Comparison: Ordinal                      Location:  -1
Comparison: OrdinalIgnoreCase            Location:  -1

Part 3: Neither start index nor count is specified.
Comparison: CurrentCulture               Location:  -1
Comparison: CurrentCultureIgnoreCase     Location:  12
Comparison: InvariantCulture             Location:  -1
Comparison: InvariantCultureIgnoreCase   Location:  12
Comparison: Ordinal                      Location:  -1
Comparison: OrdinalIgnoreCase            Location:  -1

*/


.NET Framework

受以下版本支持:4.5.2、4.5.1、4.5、4、3.5、3.0、2.0

.NET Framework Client Profile

受以下版本支持:4、3.5 SP1

可移植类库

受以下版本支持:可移植类库

适用于 Windows 应用商店应用的 .NET

受以下版本支持:Windows 8

适用于 Windows Phone 应用的 .NET

受以下版本支持:Windows Phone 8、Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008(不支持服务器核心角色), Windows Server 2008 R2(支持带 SP1 或更高版本的服务器核心角色;不支持 Itanium)

.NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参阅.NET Framework 系统要求

社区附加资源

显示:
© 2014 Microsoft