C# String LastIndexOfAny(Char[], Int32)
Description
String LastIndexOfAny(Char[], Int32)
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.
Syntax
String.LastIndexOfAny(Char[], Int32)
has the following syntax.
public int LastIndexOfAny(
char[] anyOf,
int startIndex
)
Parameters
String.LastIndexOfAny(Char[], Int32)
has the following parameters.
anyOf
- A Unicode character array containing one or more characters to seek.startIndex
- The search starting position. The search proceeds from startIndex toward the beginning of this instance.
Returns
String.LastIndexOfAny(Char[], Int32)
method returns 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 String.Empty.
Example
The following example finds the index of the last occurrence of any character in the string "is" within a substring of another string.
using System;/* w w w . j a v a2s.c om*/
class Sample {
public static void Main() {
string str = "this is a test.";
string target = "is";
char[] anyOf = target.ToCharArray();
int start = (str.Length-1)/2;
int at = str.LastIndexOfAny(anyOf, start);
if (at > -1)
Console.Write(at);
else
Console.Write("(not found)");
}
}
The code above generates the following result.