In the C# LastIndexOfAny() method is used to find index position of the last occurrence of one or more characters specified in this string or specified in a Unicode array.
Syntax
public int LastIndexOfAny(Char[] ch)
public int LastIndexOfAny(Char[], Int32)
public int LastIndexOfAny(Char[], Int32, Int32)
Parameter
ch: it is a character type array.
Return
It returns integer value.
Simple Example
using System;
public class Program
{
public static void Main()
{
string str = "findandsolve";
char[] ch = {'o','b'};
int index = str.LastIndexOfAny(ch);
Console.WriteLine(index);
}
}
Output
8
Methods | Description |
LastIndexOf(char ch) | It returns the last index of a char ch found in the String object. |
LastIndexOf(char ch, int index) | It returns the last index of a char value(if found) in the String object. The search starts at a specified index position and proceeds backwards toward the beginning of the invoked String. |
LastIndexOf(String str) | It returns the last index of a String str found in the String object. |
LastIndexOf(String str, int index) | It returns the last index of a String value(if found) in the String object. The search starts at a specified index position and proceeds backwards toward the beginning of the invoked String. |
LastIndexOf(String str, StringComparison) | It returns the last index of a String value(if found) in the String object. The search is performed on the basis of the specified field of type StringComparison. |
Example of LastIndexOf(char) method
using System;
public class Program
{
public static void Main()
{
//Creating a String object
String str1 = "Hello Find And Solve!";
//Priting the String object
Console.WriteLine("String: " + str1);
//Calling the LastIndexOf() method
int index = str1.LastIndexOf('A');
Console.WriteLine("Last Index of 'A in " + str1 + " : "+ index);
//Calling the LastIndexOf() method
index = str1.LastIndexOf('d');
Console.WriteLine("Last Index of 'd' in " + str1 + " : "+ index);
//Calling the LastIndexOf() method
index = str1.LastIndexOf('v');
Console.WriteLine("Last Index of 'V' in " + str1 + " : "+ index);
//Calling the LastIndexOf() method
index = str1.LastIndexOf('x');
Console.WriteLine("Last Index of 'F' in " + str1 + " : "+ index);
//Calling the LastIndexOf() method
index = str1.LastIndexOf('!');
Console.WriteLine("Last Index of '!' in " + str1 + " : "+ index);
}
}
Output
String: Hello Find And Solve!
Last Index of 'A in Hello Find And Solve! : 11
Last Index of 'd' in Hello Find And Solve! : 13
Last Index of 'V' in Hello Find And Solve! : 18
Last Index of 'F' in Hello Find And Solve! : -1
Last Index of '!' in Hello Find And Solve! : 20
Example of LastIndexOf(char char, int startIndex) method
using System;
public class Program
{ public static void Main()
{
//Creating a String object
String str = "Hello FindAndSolve";
//Printing the String object
Console.WriteLine("String: "+ str);
//Calling the LastIndexOf() method
int index = str.LastIndexOf('d', 6);
Console.WriteLine("Last Index of 'd' starting at index 6 in " + str + " : "+ index);
//Calling the LastIndexOf() method
index = str.LastIndexOf('V', 12);
Console.WriteLine("Last Index of 'v' starting at index 12 in " + str + " : "+ index);
//Calling the LastIndexOf() method
index = str.LastIndexOf('A', 4);
Console.WriteLine("Last Index of 'A' starting at index 4 in " + str + " : "+ index);
//Calling the LastIndexOf() method
index = str.LastIndexOf('o', 10);
Console.WriteLine("Last Index of 'o' starting at index 10 in " + str + " : "+ index);
//Calling the LastIndexOf() method index = str.LastIndexOf('S', 3);
Console.WriteLine("Last Index of 'S' starting at index 3 in " + str + " : "+ index);
}
}
Output
String: Hello FindAndSolve
Last Index of 'd' starting at index 6 in Hello FindAndSolve : -1
Last Index of 'v' starting at index 12 in Hello FindAndSolve : -1
Last Index of 'A' starting at index 4 in Hello FindAndSolve : -1
Last Index of 'o' starting at index 10 in Hello FindAndSolve : 4
Last Index of 'S' starting at index 3 in Hello FindAndSolve : -1