IndexOf 方法 (String, Int32, Int32, StringComparison)
本文由机器翻译。若要查看英语原文,请勾选“英语”复选框。 也可将鼠标指针移到文本上,在弹出窗口中显示英语原文。
翻译
英语

String.IndexOf 方法 (String, Int32, Int32, StringComparison)

 

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

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

public int IndexOf(
	string value,
	int startIndex,
	int count,
	StringComparison comparisonType
)

参数

value

要搜寻的字符串。

startIndex

搜索起始位置。

count

要检查的字符位置数。

comparisonType

指定搜索规则的枚举值之一。

返回值

Type: System.Int32

如果找到该字符串,则为 value 参数的从零开始的索引位置;如果未找到该字符串,则为 -1。如果 valueEmpty,则返回值为 startIndex

Exception Condition
ArgumentNullException

valuenull

ArgumentOutOfRangeException

countstartIndex 为负。

- 或 -

startIndex 大于此实例的长度。

- 或 -

count 大于减去此字符串的长度 startIndex

ArgumentException

comparisonType 不是有效 System.StringComparison 值。

编号从 0 (零) 开始的索引。 startIndex 参数可以介于 0 到字符串实例的长度。

在开始进行搜索 startIndex ,而且以后继续 startIndex + count -1。在字符 startIndex + count 不包括在搜索中。

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

对调用方的说明:

字符集包括可忽略字符,在执行语言性的或区分区域性的比较时该字符不被考虑。在区分区域性的搜索 (即,如果 comparisonType 不是 StringComparison.OrdinalStringComparison.OrdinalIgnoreCase) 中,如果 value 包含一个可忽略字符,则结果与移除了该字符的搜索等效。如果 value 仅包含一个或多个可忽略字符, IndexOf(String, Int32, Int32, StringComparison) 方法始终返回 startIndex, ,即开始搜索的字符位置。

在下面的示例中, IndexOf(String, Int32, Int32, StringComparison) 使用方法来查找后跟在第三到两个字符串中的第六个字符位置中启动一个"m"软连字符 (U + 00AD) 的位置。只有一个字符串包含必需的子字符串。如果在上运行该示例 .NET Framework 4 或更高版本,在这两种情况下,由于软连字符是一个可忽略字符,该方法返回的字符串中的"m"的索引执行区分区域性的比较时。当它执行序号比较时,但是,它发现的子字符串仅在第一个字符串中。请注意对于第一个字符串,包含软连字符后跟一个"m",该方法只有无法返回软连字符的索引,但在执行区分区域性的比较时,而是返回"m"的索引。只有当此方法执行序号比较时,它才会在第一个字符串中返回软连字符的索引。

using System;

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

      Console.WriteLine(s1.IndexOf(searchString, 2, 4, StringComparison.CurrentCulture));
      Console.WriteLine(s1.IndexOf(searchString, 2, 4, StringComparison.Ordinal));
      Console.WriteLine(s2.IndexOf(searchString, 2, 4, StringComparison.CurrentCulture));
      Console.WriteLine(s2.IndexOf(searchString, 2, 4, StringComparison.Ordinal));
   }
}
// The example displays the following output:
//       4
//       3
//       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

*/

Universal Windows Platform
4.5 后可用
.NET Framework
2.0 后可用
Portable Class Library
受以下版本支持:portable .NET platforms
Silverlight
2.0 后可用
Windows Phone Silverlight
7.0 后可用
Windows Phone
8.1 后可用
返回页首
显示:
© 2016 Microsoft