String.LastIndexOfAny String.LastIndexOfAny String.LastIndexOfAny String.LastIndexOfAny Method

定义

报告在 Unicode 数组中指定的一个或多个字符在此实例中的最后一个匹配项的从零开始的索引的位置。Reports the zero-based index position of the last occurrence in this instance of one or more characters specified in a Unicode array. 如果未在此实例中找到数组中的字符,则此方法返回 -1。The method returns -1 if the characters in the array are not found in this instance.

重载

LastIndexOfAny(Char[], Int32) LastIndexOfAny(Char[], Int32) LastIndexOfAny(Char[], Int32) LastIndexOfAny(Char[], Int32)

报告在 Unicode 数组中指定的一个或多个字符在此实例中的最后一个匹配项的从零开始的索引的位置。Reports the zero-based index position of the last occurrence in this instance of one or more characters specified in a Unicode array. 在指定的字符位置开始和在向后的右边该字符串的开头处理的搜索。The search starts at a specified character position and proceeds backward toward the beginning of the string.

LastIndexOfAny(Char[], Int32, Int32) LastIndexOfAny(Char[], Int32, Int32) LastIndexOfAny(Char[], Int32, Int32) LastIndexOfAny(Char[], Int32, Int32)

报告在 Unicode 数组中指定的一个或多个字符在此实例中的最后一个匹配项的从零开始的索引的位置。Reports the zero-based index position of the last occurrence in this instance of one or more characters specified in a Unicode array. 搜索在指定字符位置的数目的字符串开始时,开始指定字符和其后面的位置。The search starts at a specified character position and proceeds backward toward the beginning of the string for a specified number of character positions.

LastIndexOfAny(Char[]) LastIndexOfAny(Char[]) LastIndexOfAny(Char[]) LastIndexOfAny(Char[])

报告在 Unicode 数组中指定的一个或多个字符在此实例中的最后一个匹配项的从零开始的索引的位置。Reports the zero-based index position of the last occurrence in this instance of one or more characters specified in a Unicode array.

LastIndexOfAny(Char[], Int32) LastIndexOfAny(Char[], Int32) LastIndexOfAny(Char[], Int32) LastIndexOfAny(Char[], Int32)

报告在 Unicode 数组中指定的一个或多个字符在此实例中的最后一个匹配项的从零开始的索引的位置。Reports the zero-based index position of the last occurrence in this instance of one or more characters specified in a Unicode array. 在指定的字符位置开始和在向后的右边该字符串的开头处理的搜索。The search starts at a specified character position and proceeds backward toward the beginning of the string.

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

参数

anyOf
Char[]

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

startIndex
Int32 Int32 Int32 Int32

搜索起始位置。The search starting position. startIndex 此实例的开头开始搜索。The search proceeds from startIndex toward the beginning of this instance.

返回

最后一次在此实例中找到 anyOf 中的任意字符的索引位置;如果未找到 anyOf 中的字符或者当前实例等于Empty,则为 -1。The index position of the last occurrence in this instance where any character in anyOf was found; -1 if no character in anyOf was found or if the current instance equals Empty.

异常

当前实例不等于 Empty,并且 startIndex 指定不在此实例内的位置。The current instance does not equal Empty, and startIndex specifies a position that is not within this instance.

示例

下面的示例查找字符串 "is" 中任何字符的最后一个匹配项的索引, 该字符串位于另一个字符串的子字符串中。The following example finds the index of the last occurrence of any character in the string "is" within a substring of another string.

// Sample for String::LastIndexOfAny(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 - 1) / 2;
   Console::WriteLine( "The last character occurrence  from position {0} to 0.", start );
   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->LastIndexOfAny( anyOf, start );
   if ( at > -1 )
      Console::Write( at );
   else
      Console::Write( "(not found)" );

   Console::Write( "{0}{0}{0}", Environment::NewLine );
}

/*
This example produces the following results:
The last character occurrence  from position 33 to 0.
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: 12


*/
// Sample for String.LastIndexOfAny(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-1)/2;
    Console.WriteLine("The last character occurrence  from position {0} to 0.", start);
    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.LastIndexOfAny(anyOf, start);
    if (at > -1) 
        Console.Write(at);
    else
        Console.Write("(not found)");
    Console.Write("{0}{0}{0}", Environment.NewLine);
    }
}
/*
This example produces the following results:
The last character occurrence  from position 33 to 0.
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: 12


*/
' Sample for String.LastIndexOfAny(Char[], Int32)
Imports System
 _

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 - 1) / 2
      Console.WriteLine("The last character occurrence  from position {0} to 0.", start)
      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.LastIndexOfAny(anyOf, start)
      If at > - 1 Then
         Console.Write(at)
      Else
         Console.Write("(not found)")
      End If
      Console.Write("{0}{0}{0}", Environment.NewLine)
   End Sub 'Main
End Class 'Sample
'
'This example produces the following results:
'The last character occurrence  from position 33 to 0.
'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: 12
'
'
'

注解

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

此方法从该实例的startIndex字符位置开始搜索, 并在开始时向后移动, 直到找到中anyOf的字符或检查了第一个字符位置。This method begins searching at the startIndex character position of this instance and proceeds backward toward the beginning until either a character in anyOf is found or the first character position has been examined. 搜索区分大小写。The search 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 values are the same. 若要执行区分区域性的搜索, 请使用CompareInfo.LastIndexOf方法, 其中表示预构成字符的 Unicode 标量值 (如连字 "Æ" (U + 00C6)) 可能被视为等效于正确的序列, 如 "AE" (U + 0041, U + 0045), 具体取决于区域性。To perform a culture-sensitive search, use the CompareInfo.LastIndexOf 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.

另请参阅

LastIndexOfAny(Char[], Int32, Int32) LastIndexOfAny(Char[], Int32, Int32) LastIndexOfAny(Char[], Int32, Int32) LastIndexOfAny(Char[], Int32, Int32)

报告在 Unicode 数组中指定的一个或多个字符在此实例中的最后一个匹配项的从零开始的索引的位置。Reports the zero-based index position of the last occurrence in this instance of one or more characters specified in a Unicode array. 搜索在指定字符位置的数目的字符串开始时,开始指定字符和其后面的位置。The search starts at a specified character position and proceeds backward toward the beginning of the string for a specified number of character positions.

public:
 int LastIndexOfAny(cli::array <char> ^ anyOf, int startIndex, int count);
public int LastIndexOfAny (char[] anyOf, int startIndex, int count);
member this.LastIndexOfAny : char[] * int * int -> int
Public Function LastIndexOfAny (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 Int32 Int32 Int32

搜索起始位置。The search starting position. startIndex 此实例的开头开始搜索。The search proceeds from startIndex toward the beginning of this instance.

count
Int32 Int32 Int32 Int32

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

返回

最后一次在此实例中找到 anyOf 中的任意字符的索引位置;如果未找到 anyOf 中的字符或者当前实例等于Empty,则为 -1。The index position of the last occurrence in this instance where any character in anyOf was found; -1 if no character in anyOf was found or if the current instance equals Empty.

异常

当前实例不等于 Empty,并且 countstartIndex 为负数。The current instance does not equal Empty, and count or startIndex is negative.

-or- 当前实例不等于 Empty,并且 startIndex 减去 count 再加 1 小于零。The current instance does not equal Empty, and startIndex minus count + 1 is less than zero.

示例

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

// Sample for String::LastIndexOfAny(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) * 2) / 3;
   count = (str->Length - 1) / 3;
   Console::WriteLine( "The last 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->LastIndexOfAny( anyOf, start, count );
   if ( at > -1 )
      Console::Write( at );
   else
      Console::Write( "(not found)" );

   Console::Write( "{0}{0}{0}", Environment::NewLine );
}

/*
This example produces the following results:
The last character occurrence from position 44 for 22 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.LastIndexOfAny(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)*2)/3;
    count = (str.Length-1)/3;
    Console.WriteLine("The last 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.LastIndexOfAny(anyOf, start, count);
    if (at > -1) 
        Console.Write(at);
    else
        Console.Write("(not found)");
    Console.Write("{0}{0}{0}", Environment.NewLine);
    }
}
/*
This example produces the following results:
The last character occurrence from position 44 for 22 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.LastIndexOfAny(Char[], Int32, Int32)
Imports System
 _

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) * 2 / 3
      count =(str.Length - 1) / 3
      Console.WriteLine("The last 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.LastIndexOfAny(anyOf, start, count)
      If at > - 1 Then
         Console.Write(at)
      Else
         Console.Write("(not found)")
      End If
      Console.Write("{0}{0}{0}", Environment.NewLine)
   End Sub 'Main
End Class 'Sample
'
'This example produces the following results:
'The last character occurrence from position 44 for 22 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
'

注解

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

此方法从该实例的startIndex字符位置开始搜索, 并在开始时向后移动, 直到找到了anyOf中的一个count字符或已检查了个字符位置。This method begins searching at the startIndex character position of this instance and proceeds backward toward the beginning until either a character in anyOf is found or count character positions have been examined. 搜索区分大小写。The search 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 values are the same. 若要执行区分区域性的搜索, 请使用CompareInfo.LastIndexOf方法, 其中表示预构成字符的 Unicode 标量值 (如连字 "Æ" (U + 00C6)) 可能被视为等效于正确的序列, 如 "AE" (U + 0041, U + 0045), 具体取决于区域性。To perform a culture-sensitive search, use the CompareInfo.LastIndexOf 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.

另请参阅

LastIndexOfAny(Char[]) LastIndexOfAny(Char[]) LastIndexOfAny(Char[]) LastIndexOfAny(Char[])

报告在 Unicode 数组中指定的一个或多个字符在此实例中的最后一个匹配项的从零开始的索引的位置。Reports the zero-based index position of the last occurrence in this instance of one or more characters specified in a Unicode array.

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

参数

anyOf
Char[]

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

返回

最后一次在此实例中找到 anyOf 中的任意字符的索引位置;如果未找到 anyOf 中的字符,则为 -1。The index position of the last occurrence in this instance where any character in anyOf was found; -1 if no character in anyOf was found.

异常

示例

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

// Sample for String::LastIndexOfAny(Char[])
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 - 1;
   Console::WriteLine( "The last character occurrence  from position {0} to 0.", start );
   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->LastIndexOfAny( anyOf );
   if ( at > -1 )
      Console::Write( at );
   else
      Console::Write( "(not found)" );

   Console::Write( "{0}{0}{0}", Environment::NewLine );
}

/*
This example produces the following results:
The last character occurrence  from position 66 to 0.
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: 58


*/
// Sample for String.LastIndexOfAny(Char[])
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-1;
    Console.WriteLine("The last character occurrence  from position {0} to 0.", start);
    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.LastIndexOfAny(anyOf);
    if (at > -1) 
        Console.Write(at);
    else
        Console.Write("(not found)");
    Console.Write("{0}{0}{0}", Environment.NewLine);
    }
}
/*
This example produces the following results:
The last character occurrence  from position 66 to 0.
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: 58


*/
' Sample for String.LastIndexOfAny(Char[])
Imports System
 _

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 - 1
      Console.WriteLine("The last character occurrence  from position {0} to 0.", start)
      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.LastIndexOfAny(anyOf)
      If at > - 1 Then
         Console.Write(at)
      Else
         Console.Write("(not found)")
      End If
      Console.Write("{0}{0}{0}", Environment.NewLine)
   End Sub 'Main
End Class 'Sample
'
'This example produces the following results:
'The last character occurrence  from position 66 to 0.
'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: 58
'
'
'

注解

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

此方法从该实例的最后一个字符位置开始搜索, 并在开始时向后继续, 直到找到anyOf中的字符或检查了第一个字符位置。This method begins searching at the last character position of this instance and proceeds backward toward the beginning until either a character in anyOf is found or the first character position has been examined. 搜索区分大小写。The search 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 values are the same. 若要执行区分区域性的搜索, 请使用CompareInfo.LastIndexOf方法, 其中表示预构成字符的 Unicode 标量值 (如连字 "Æ" (U + 00C6)) 可能被视为等效于正确的序列, 如 "AE" (U + 0041, U + 0045), 具体取决于区域性。To perform a culture-sensitive search, use the CompareInfo.LastIndexOf 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.

另请参阅

适用于