Mono Class Library: System.String Overview | Members

System.String.LastIndexOf Method

Returns the index of the last occurrence of a specified character within the current instance.

public int LastIndexOf (char value, int startIndex)

Parameters

value
A Unicode character to locate.
startIndex
A int containing the index in the current instance from which to begin searching.

Returns

A int containing the index of the last occurrence of value in the current instance, if found; otherwise, -1.

Exceptions

TypeReason
ArgumentNullExceptionvalue is a null reference.
ArgumentOutOfRangeExceptionstartIndex is less than zero or greater than the length of the current instance.

Remarks

This method searches for the last occurrence of the specified character between the start of the string and the indicated index.

This method is case-sensitive.

Example

The following example demonstrates the string.LastIndexOf(char) method.

C# Example

using System;
public class StringLastIndexOfTest {
   public static void Main() {
      String str = "aa bb cc dd";
      
      Console.WriteLine( str.LastIndexOf('d', 8) );
      Console.WriteLine( str.LastIndexOf('b', 8) );
   }
}

The output is

-1
4

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0