String.IndexOfAny 方法

定义

报告指定 Unicode 字符数组中的任意字符在此实例中第一个匹配项的索引。Reports the index of the first occurrence in this instance of any character in a specified array of Unicode characters. 如果未在此实例中找到数组中的字符,则此方法返回 -1。The method returns -1 if the characters in the array are not found in this instance.

重载

IndexOfAny(Char[])

报告指定 Unicode 字符数组中的任意字符在此实例中第一个匹配项的从零开始的索引。Reports the zero-based index of the first occurrence in this instance of any character in a specified array of Unicode characters.

IndexOfAny(Char[], Int32)

报告指定 Unicode 字符数组中的任意字符在此实例中第一个匹配项的从零开始的索引。Reports the zero-based index of the first occurrence in this instance of any character in a specified array of Unicode characters. 该搜索从指定字符位置开始。The search starts at a specified character position.

IndexOfAny(Char[], Int32, Int32)

报告指定 Unicode 字符数组中的任意字符在此实例中第一个匹配项的从零开始的索引。Reports the zero-based index of the first occurrence in this instance of any character in a specified array of Unicode characters. 搜索从指定字符位置开始,并检查指定数量的字符位置。The search starts at a specified character position and examines a specified number of character positions.

IndexOfAny(Char[])

报告指定 Unicode 字符数组中的任意字符在此实例中第一个匹配项的从零开始的索引。Reports the zero-based index of the first occurrence in this instance of any character in a specified array of Unicode characters.

public:
 int IndexOfAny(cli::array <char> ^ anyOf);
public int IndexOfAny (char[] anyOf);
member this.IndexOfAny : char[] -> int
Public Function IndexOfAny (anyOf As Char()) As Integer

参数

anyOf
Char[]

Unicode 字符数组,包含一个或多个要查找的字符。A Unicode character array containing one or more characters to seek.

返回

在此实例中第一次找到 anyOf 中的任意字符的索引位置(从零开始);如果未找到 anyOf 中的字符,则为 -1。The zero-based index position of the first occurrence in this instance where any character in anyOf was found; -1 if no character in anyOf was found.

异常

anyOfnullanyOf is null.

示例

下面的示例查找字符串中的第一个元音。The following example finds the first vowel in a string.

using System;

public class Example
{
   public static void Main()
   {
      char[] chars = { 'a', 'e', 'i', 'o', 'u', 'y', 
                       'A', 'E', 'I', 'O', 'U', 'Y' };
      String s = "The long and winding road...";
      Console.WriteLine("The first vowel in \n   {0}\nis found at position {1}", 
                        s, s.IndexOfAny(chars) + 1);                         
   }
}
// The example displays the following output:
//       The first vowel in
//          The long and winding road...
//       is found at position 3
Module Example
   Public Sub Main()
      Dim chars() As Char = { "a"c, "e"c, "i"c, "o"c, "u"c, "y"c, 
                              "A"c, "E"c, "I"c, "O"c, "U"c, "Y"c }
      Dim s As String = "The long and winding road..."
      Console.WriteLine("The first vowel in {2}   {0}{2}is found at position {1}", 
                        s, s.IndexOfAny(chars) + 1, vbCrLf)                         
   End Sub
End Module
' The example displays the following output:
'       The first vowel in
'          The long and winding road...
'       is found at position 3

注解

索引编号从0开始。Index numbering starts from zero.

搜索 anyOf 区分大小写。The search for anyOf is case-sensitive. 如果 anyOf 为空数组,该方法将在字符串的开头找到匹配项(即索引为零)。If anyOf is an empty array, the method finds a match at the beginning of the string (that is, at index zero).

此方法执行序号(不区分区域性)搜索,其中,仅当字符的 Unicode 标量值相同时,才将其视为等效于另一个字符。This method performs an ordinal (culture-insensitive) search, where a character is considered equivalent to another character only if their Unicode scalar values are the same. 若要执行区分区域性的搜索,请使用 CompareInfo.IndexOf 方法,其中表示预构成字符的 Unicode 标量值(如连字 "Æ" (U + 00C6))可能被视为与正确序列,如 "AE" (U + 0041,U + 0045),具体取决于区域性。To perform a culture-sensitive search, use the CompareInfo.IndexOf method, where a Unicode scalar value representing a precomposed character, such as the ligature "Æ" (U+00C6), might be considered equivalent to any occurrence of the character's components in the correct sequence, such as "AE" (U+0041, U+0045), depending on the culture.

另请参阅

IndexOfAny(Char[], Int32)

报告指定 Unicode 字符数组中的任意字符在此实例中第一个匹配项的从零开始的索引。Reports the zero-based index of the first occurrence in this instance of any character in a specified array of Unicode characters. 该搜索从指定字符位置开始。The search starts at a specified character position.

public:
 int IndexOfAny(cli::array <char> ^ anyOf, int startIndex);
public int IndexOfAny (char[] anyOf, int startIndex);
member this.IndexOfAny : char[] * int -> int
Public Function IndexOfAny (anyOf As Char(), startIndex As Integer) As Integer

参数

anyOf
Char[]

Unicode 字符数组,包含一个或多个要查找的字符。A Unicode character array containing one or more characters to seek.

startIndex
Int32

搜索起始位置。The search starting position.

返回

在此实例中第一次找到 anyOf 中的任意字符的索引位置(从零开始);如果未找到 anyOf 中的字符,则为 -1。The zero-based index position of the first occurrence in this instance where any character in anyOf was found; -1 if no character in anyOf was found.

异常

anyOfnullanyOf is null.

startIndex 为负数。startIndex is negative.

-or- startIndex 大于此实例中的字符数。startIndex is greater than the number of characters in this instance.

示例

下面的示例查找字符串 "is" 在另一个字符串的子字符串中的匹配项的索引。The following example finds the index of the occurrence of any character of the string "is" within a substring of another string.

// Sample for String::IndexOfAny(Char[], Int32)
using namespace System;
int main()
{
   String^ br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-";
   String^ br2 = "0123456789012345678901234567890123456789012345678901234567890123456";
   String^ str = "Now is the time for all good men to come to the aid of their party.";
   int start;
   int at;
   String^ target = "is";
   array<Char>^anyOf = target->ToCharArray();
   start = str->Length / 2;
   Console::WriteLine();
   Console::WriteLine( "The first character occurrence from position {0} to {1}.", start, str->Length - 1 );
   Console::WriteLine( "{1}{0}{2}{0}{3}{0}", Environment::NewLine, br1, br2, str );
   Console::Write( "A character in '{0}' occurs at position: ", target );
   at = str->IndexOfAny( anyOf, start );
   if ( at > -1 )
      Console::Write( at );
   else
      Console::Write( "(not found)" );

   Console::WriteLine();
}

/*

The first character occurrence from position 33 to 66.
0----+----1----+----2----+----3----+----4----+----5----+----6----+-
0123456789012345678901234567890123456789012345678901234567890123456
Now is the time for all good men to come to the aid of their party.

A character in 'is' occurs at position: 49

*/
// Sample for String.IndexOfAny(Char[], Int32)
using System;

class Sample {
    public static void Main() 
    {
    string br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-";
    string br2 = "0123456789012345678901234567890123456789012345678901234567890123456";
    string str = "Now is the time for all good men to come to the aid of their party.";
    int start;
    int at;
    string target = "is";
    char[] anyOf = target.ToCharArray();

    start = str.Length/2;
    Console.WriteLine();
    Console.WriteLine("The first character occurrence from position {0} to {1}.", 
                           start, str.Length-1);
    Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str);
    Console.Write("A character in '{0}' occurs at position: ", target);

    at = str.IndexOfAny(anyOf, start);
    if (at > -1) 
        Console.Write(at);
    else
        Console.Write("(not found)");
    Console.WriteLine();
    }
}
/*

The first character occurrence from position 33 to 66.
0----+----1----+----2----+----3----+----4----+----5----+----6----+-
0123456789012345678901234567890123456789012345678901234567890123456
Now is the time for all good men to come to the aid of their party.

A character in 'is' occurs at position: 49

*/
' Sample for String.IndexOfAny(Char[], Int32)
Class Sample
   Public Shared Sub Main()
      Dim br1 As String = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-"
      Dim br2 As String = "0123456789012345678901234567890123456789012345678901234567890123456"
      Dim str As String = "Now is the time for all good men to come to the aid of their party."
      Dim start As Integer
      Dim at As Integer
      Dim target As String = "is"
      Dim anyOf As Char() = target.ToCharArray()
      
      start = str.Length / 2
      Console.WriteLine()
      Console.WriteLine("Search for a character occurrence from position {0} to {1}.", _
                           start, str.Length - 1)
      Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str)
      Console.Write("A character in '{0}' occurs at position: ", target)
      at = str.IndexOfAny(anyOf, start)
      If at > - 1 Then
         Console.Write(at)
      Else
         Console.Write("(not found)")
      End If
      Console.WriteLine()
   End Sub
End Class
'
'
'Search for a character occurrence from position 33 to 66.
'0----+----1----+----2----+----3----+----4----+----5----+----6----+-
'0123456789012345678901234567890123456789012345678901234567890123456
'Now is the time for all good men to come to the aid of their party.
'
'A character in 'is' occurs at position: 49
'

注解

索引编号从0开始。Index numbering starts from zero. startIndex 参数的范围可以介于0到字符串实例的长度之间。The startIndex parameter can range from 0 to one less than the length of the string instance.

搜索范围从 startIndex 到字符串的末尾。The search ranges from startIndex to the end of the string.

搜索 anyOf 区分大小写。The search for anyOf is case-sensitive.

此方法执行序号(不区分区域性)搜索,其中字符仅在其 Unicode 标量值相同时才被视为等效于另一个字符。This method performs an ordinal (culture-insensitive) search, where a character is considered equivalent to another character only if their Unicode scalar value are the same. 若要执行区分区域性的搜索,请使用 CompareInfo.IndexOf 方法,其中表示预构成字符的 Unicode 标量值(如连字 "Æ" (U + 00C6))可能被视为与正确序列,如 "AE" (U + 0041,U + 0045),具体取决于区域性。To perform a culture-sensitive search, use the CompareInfo.IndexOf method, where a Unicode scalar value representing a precomposed character, such as the ligature "Æ" (U+00C6), might be considered equivalent to any occurrence of the character's components in the correct sequence, such as "AE" (U+0041, U+0045), depending on the culture.

另请参阅

IndexOfAny(Char[], Int32, Int32)

报告指定 Unicode 字符数组中的任意字符在此实例中第一个匹配项的从零开始的索引。Reports the zero-based index of the first occurrence in this instance of any character in a specified array of Unicode characters. 搜索从指定字符位置开始,并检查指定数量的字符位置。The search starts at a specified character position and examines a specified number of character positions.

public:
 int IndexOfAny(cli::array <char> ^ anyOf, int startIndex, int count);
public int IndexOfAny (char[] anyOf, int startIndex, int count);
member this.IndexOfAny : char[] * int * int -> int
Public Function IndexOfAny (anyOf As Char(), startIndex As Integer, count As Integer) As Integer

参数

anyOf
Char[]

Unicode 字符数组,包含一个或多个要查找的字符。A Unicode character array containing one or more characters to seek.

startIndex
Int32

搜索起始位置。The search starting position.

count
Int32

要检查的字符位置数。The number of character positions to examine.

返回

在此实例中第一次找到 anyOf 中的任意字符的索引位置(从零开始);如果未找到 anyOf 中的字符,则为 -1。The zero-based index position of the first occurrence in this instance where any character in anyOf was found; -1 if no character in anyOf was found.

异常

anyOfnullanyOf is null.

countstartIndex 为负数。count or startIndex is negative.

-or- count + startIndex 大于此实例中的字符数。count + startIndex is greater than the number of characters in this instance.

示例

下面的示例查找字符串 "辅助" 的任何字符在另一个字符串的子字符串中的匹配项的索引。The following example finds the index of the occurrence of any character of the string "aid" within a substring of another string.

// Sample for String::IndexOfAny(Char[], Int32, Int32)
using namespace System;
int main()
{
   String^ br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-";
   String^ br2 = "0123456789012345678901234567890123456789012345678901234567890123456";
   String^ str = "Now is the time for all good men to come to the aid of their party.";
   int start;
   int at;
   int count;
   String^ target = "aid";
   array<Char>^anyOf = target->ToCharArray();
   start = (str->Length - 1) / 3;
   count = (str->Length - 1) / 4;
   Console::WriteLine();
   Console::WriteLine( "The first character occurrence from position {0} for {1} characters.", start, count );
   Console::WriteLine( "{1}{0}{2}{0}{3}{0}", Environment::NewLine, br1, br2, str );
   Console::Write( "A character in '{0}' occurs at position: ", target );
   at = str->IndexOfAny( anyOf, start, count );
   if ( at > -1 )
      Console::Write( at );
   else
      Console::Write( "(not found)" );

   Console::WriteLine();
}

/*

The first character occurrence from position 22 for 16 characters.
0----+----1----+----2----+----3----+----4----+----5----+----6----+-
0123456789012345678901234567890123456789012345678901234567890123456
Now is the time for all good men to come to the aid of their party.

A character in 'aid' occurs at position: 27

*/
// Sample for String.IndexOfAny(Char[], Int32, Int32)
using System;

class Sample {
    public static void Main() 
    {
    string br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-";
    string br2 = "0123456789012345678901234567890123456789012345678901234567890123456";
    string str = "Now is the time for all good men to come to the aid of their party.";
    int start;
    int at;
    int count;
    string target = "aid";
    char[] anyOf = target.ToCharArray();

    start = (str.Length-1)/3;
    count = (str.Length-1)/4;
    Console.WriteLine();
    Console.WriteLine("The first character occurrence from position {0} for {1} characters.", start, count);
    Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str);
    Console.Write("A character in '{0}' occurs at position: ", target);

    at = str.IndexOfAny(anyOf, start, count);
    if (at > -1) 
        Console.Write(at);
    else
        Console.Write("(not found)");
    Console.WriteLine();
    }
}
/*

The first character occurrence from position 22 for 16 characters.
0----+----1----+----2----+----3----+----4----+----5----+----6----+-
0123456789012345678901234567890123456789012345678901234567890123456
Now is the time for all good men to come to the aid of their party.

A character in 'aid' occurs at position: 27

*/
' Sample for String.IndexOfAny(Char[], Int32, Int32)
Class Sample
   Public Shared Sub Main()
      Dim br1 As String = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-"
      Dim br2 As String = "0123456789012345678901234567890123456789012345678901234567890123456"
      Dim str As String = "Now is the time for all good men to come to the aid of their party."
      Dim start As Integer
      Dim at As Integer
      Dim count As Integer
      Dim target As String = "aid"
      Dim anyOf As Char() = target.ToCharArray()
      
      start =(str.Length - 1) / 3
      count =(str.Length - 1) / 4
      Console.WriteLine()
      Console.WriteLine("The first character occurrence from position {0} for {1} characters.", start, count)
      Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str)
      Console.Write("A character in '{0}' occurs at position: ", target)
      
      at = str.IndexOfAny(anyOf, start, count)
      If at > - 1 Then
         Console.Write(at)
      Else
         Console.Write("(not found)")
      End If
      Console.WriteLine()
   End Sub
End Class
'
'The first character occurrence from position 22 for 16 characters.
'0----+----1----+----2----+----3----+----4----+----5----+----6----+-
'0123456789012345678901234567890123456789012345678901234567890123456
'Now is the time for all good men to come to the aid of their party.
'
'A character in 'aid' occurs at position: 27
'

注解

搜索从 startIndex 开始,并继续 + count-1 startIndexThe search begins at startIndex and continues to startIndex + count -1. 搜索中不包括 startIndex + count 的字符。The character at startIndex + count is not included in the search.

索引编号从0开始。Index numbering starts from zero. startIndex 参数的范围可以介于0到字符串实例的长度之间。The startIndex parameter can range from 0 to one less than the length of the string instance.

搜索 anyOf 区分大小写。The search for anyOf is case-sensitive.

此方法执行序号(不区分区域性)搜索,其中字符仅在其 Unicode 标量值相同时才被视为等效于另一个字符。This method performs an ordinal (culture-insensitive) search, where a character is considered equivalent to another character only if their Unicode scalar value are the same. 若要执行区分区域性的搜索,请使用 CompareInfo.IndexOf 方法,其中表示预构成字符的 Unicode 标量值(如连字 "Æ" (U + 00C6))可能被视为与正确序列,如 "AE" (U + 0041,U + 0045),具体取决于区域性。To perform a culture-sensitive search, use the CompareInfo.IndexOf method, where a Unicode scalar value representing a precomposed character, such as the ligature "Æ" (U+00C6), might be considered equivalent to any occurrence of the character's components in the correct sequence, such as "AE" (U+0041, U+0045), depending on the culture.

另请参阅

适用于