C# String LastIndexOf(String, Int32, Int32)
Description
String LastIndexOf(String, Int32, Int32)
reports the
zero-based index position of the last occurrence of a specified string within
this instance. The search starts at a specified character position and proceeds
backward toward the beginning of the string for a specified number of character
positions.
Syntax
String.LastIndexOf(String, Int32, Int32)
has the following syntax.
public int LastIndexOf(
string value,/* w w w . j ava 2 s . c o m*/
int startIndex,
int count
)
Parameters
String.LastIndexOf(String, Int32, Int32)
has the following parameters.
value
- The string to seek.startIndex
- The search starting position. The search proceeds from startIndex toward the beginning of this instance.count
- The number of character positions to examine.
Returns
String.LastIndexOf(String, Int32, Int32)
method returns The zero-based starting index position of value if that string is found,
or -1 if it is not found or if the current instance equals String.Empty. If
value is Empty, the return value is the smaller of startIndex and the last
index position in this instance.
Example
using System;//from w w w . j a v a 2 s .c o m
public class Example
{
public static void Main()
{
int position = 0;
string s1 = "ani\u00ADmal";
string s2 = "animal";
// Find the index of the soft hyphen followed by "n".
position = s1.LastIndexOf("m");
Console.WriteLine("'m' at position {0}", position);
if (position >= 0)
Console.WriteLine(s1.LastIndexOf("\u00ADn", position, position + 1));
}
}
The code above generates the following result.