C# String LastIndexOf(Char, Int32)
Description
String LastIndexOf(Char, Int32)
reports the zero-based
index position of the last occurrence of a specified Unicode character within
this instance. The search starts at a specified character position and proceeds
backward toward the beginning of the string.
Syntax
String.LastIndexOf(Char, Int32)
has the following syntax.
public int LastIndexOf(
char value,
int startIndex
)
Parameters
String.LastIndexOf(Char, Int32)
has the following parameters.
value
- The Unicode character to seek.startIndex
- The starting position of the search. The search proceeds from startIndex toward the beginning of this instance.
Returns
String.LastIndexOf(Char, Int32)
method returns The zero-based index position of value if that character is found, or -1 if
it is not found or if the current instance equals String.Empty.
Example
Sample for String.LastIndexOf(Char, Int32)
// w w w .j a va 2 s. co m
using System;
class Sample {
public static void Main() {
string str = "this is a test.";
int start = str.Length-1;
int at = 0;
while((start > -1) && (at > -1)){
at = str.LastIndexOf('t', start);
if (at > -1) {
Console.Write("{0} ", at);
start = at - 1;
}
}
}
}
The code above generates the following result.