String.LastIndexOfAny 方法

定义

报告在 Unicode 数组中指定的一个或多个字符在此实例中的最后一个匹配项的从零开始的索引的位置。 如果未在此实例中找到数组中的字符,则此方法返回 -1。

重载

LastIndexOfAny(Char[])

报告在 Unicode 数组中指定的一个或多个字符在此实例中的最后一个匹配项的从零开始的索引的位置。

LastIndexOfAny(Char[], Int32)

报告在 Unicode 数组中指定的一个或多个字符在此实例中的最后一个匹配项的从零开始的索引的位置。 在指定的字符位置开始和在向后的右边该字符串的开头处理的搜索。

LastIndexOfAny(Char[], Int32, Int32)

报告在 Unicode 数组中指定的一个或多个字符在此实例中的最后一个匹配项的从零开始的索引的位置。 搜索在指定字符位置的数目的字符串开始时,开始指定字符和其后面的位置。

LastIndexOfAny(Char[])

报告在 Unicode 数组中指定的一个或多个字符在此实例中的最后一个匹配项的从零开始的索引的位置。

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 字符数组,包含一个或多个要查找的字符。

返回

最后一次在此实例中找到 anyOf 中的任意字符的索引位置;如果未找到 anyOf 中的字符,则为 -1。

例外

anyOf 上声明的默认值为 null

示例

以下示例查找另一个字符串中字符串“is”中任何字符的最后一个匹配项的索引。

// 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[])
open System

let br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-"
let br2 = "0123456789012345678901234567890123456789012345678901234567890123456"
let str = "Now is the time for all good men to come to the aid of their party."
let target = "is"
let anyOf = target.ToCharArray()

let start = str.Length - 1
printfn $"The last character occurrence  from position {start} to 0."
printfn $"{br1}{Environment.NewLine}{br2}{Environment.NewLine}{str}{Environment.NewLine}"
printf $"A character in '{target}' occurs at position: "

let at = str.LastIndexOfAny anyOf
if at > -1 then
    printf $"{at}"
else
    printf "(not found)"
printf $"{Environment.NewLine}{Environment.NewLine}{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[])
 _

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
End Class
'
'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
'
'
'

注解

索引编号从零开始。

此方法从此实例的最后一个字符位置开始搜索,然后向后向开头搜索,直到找到 中的 anyOf 字符或检查第一个字符位置。 搜索区分大小写。

此方法执行不区分区域性的序号 () 搜索,其中仅当一个字符的 Unicode 标量值相同时,才认为该字符等效于另一个字符。 若要执行区分区域性的搜索,请使用 CompareInfo.LastIndexOf 方法,其中表示预分解字符的 Unicode 标量值(如连字“Æ” (U+00C6) )可能被视为等效于字符组件在正确序列中的任何匹配项,例如“AE” (U+0041、U+0045) ,具体取决于区域性。

另请参阅

适用于

LastIndexOfAny(Char[], Int32)

报告在 Unicode 数组中指定的一个或多个字符在此实例中的最后一个匹配项的从零开始的索引的位置。 在指定的字符位置开始和在向后的右边该字符串的开头处理的搜索。

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 字符数组,包含一个或多个要查找的字符。

startIndex
Int32

搜索起始位置。 从 startIndex 此实例的开头开始搜索。

返回

最后一次在此实例中找到 anyOf 中的任意字符的索引位置;如果未找到 anyOf 中的字符或者当前实例等于Empty,则为 -1。

例外

anyOf 上声明的默认值为 null

当前实例不等于 Empty,并且 startIndex 指定不在此实例内的位置。

示例

以下示例在另一个字符串的子字符串中查找字符串“is”中任何字符的最后一个匹配项的索引。

// 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)
open System

let br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-"
let br2 = "0123456789012345678901234567890123456789012345678901234567890123456"
let str = "Now is the time for all good men to come to the aid of their party."
let target = "is"
let anyOf = target.ToCharArray()

let start = (str.Length - 1) / 2
printfn $"The last character occurrence  from position {start} to 0."
printfn $"{br1}{Environment.NewLine}{br2}{Environment.NewLine}{str}{Environment.NewLine}"
printf $"A character in '{target}' occurs at position: "

let at = str.LastIndexOfAny(anyOf, start)
if at > -1 then
    printf $"{at}"
else
    printf "(not found)"
printf $"{Environment.NewLine}{Environment.NewLine}{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)
 _

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
End Class
'
'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
'
'
'

注解

索引编号从零开始。

此方法开始搜索此实例的 startIndex 字符位置,然后向后向开头搜索,直到找到 中的 anyOf 字符或已检查第一个字符位置。 搜索区分大小写。

此方法执行不区分区域性的序号 () 搜索,其中仅当一个字符的 Unicode 标量值相同时,才认为该字符等效于另一个字符。 若要执行区分区域性的搜索,请使用 CompareInfo.LastIndexOf 方法,其中表示预分解字符的 Unicode 标量值(如连字“Æ” (U+00C6) )可能被视为等效于字符组件在正确序列中的任何匹配项,例如“AE” (U+0041、U+0045) ,具体取决于区域性。

另请参阅

适用于

LastIndexOfAny(Char[], Int32, Int32)

报告在 Unicode 数组中指定的一个或多个字符在此实例中的最后一个匹配项的从零开始的索引的位置。 搜索在指定字符位置的数目的字符串开始时,开始指定字符和其后面的位置。

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 字符数组,包含一个或多个要查找的字符。

startIndex
Int32

搜索起始位置。 从 startIndex 此实例的开头开始搜索。

count
Int32

要检查的字符位置数。

返回

最后一次在此实例中找到 anyOf 中的任意字符的索引位置;如果未找到 anyOf 中的字符或者当前实例等于Empty,则为 -1。

例外

anyOf 上声明的默认值为 null

当前实例不等于 Empty,并且 countstartIndex 为负数。

当前实例不等于 Empty,并且 startIndex 减去 count 再加 1 小于零。

示例

以下示例在另一个字符串的子字符串中查找字符串“aid”中任何字符最后一个匹配项的索引。

// 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)
open System

let br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-"
let br2 = "0123456789012345678901234567890123456789012345678901234567890123456"
let str = "Now is the time for all good men to come to the aid of their party."
let target = "aid"
let anyOf = target.ToCharArray()

let start = ((str.Length - 1) * 2) / 3
let count = (str.Length - 1) / 3
printfn $"The last character occurrence from position {start} for {count} characters."
printfn $"{br1}{Environment.NewLine}{br2}{Environment.NewLine}{str}{Environment.NewLine}"
printf $"A character in '{target}' occurs at position: "

let at = str.LastIndexOfAny(anyOf, start, count)
if at > -1 then
    printf $"{at}"
else
    printf "(not found)"
printf $"{Environment.NewLine}{Environment.NewLine}{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)
 _

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
End Class
'
'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
'

注解

索引编号从零开始。

此方法开始搜索此实例的 startIndex 字符位置,然后向后向开头搜索,直到找到 中的 anyOf 字符或 count 检查字符位置。 搜索区分大小写。

此方法执行不区分区域性的序号 () 搜索,其中仅当一个字符的 Unicode 标量值相同时,才认为该字符等效于另一个字符。 若要执行区分区域性的搜索,请使用 CompareInfo.LastIndexOf 方法,其中表示预分解字符的 Unicode 标量值(如连字“Æ” (U+00C6) )可能被视为等效于字符组件在正确序列中的任何匹配项,例如“AE” (U+0041、U+0045) ,具体取决于区域性。

另请参阅

适用于