C# String LastIndexOf(Char, Int32, Int32)

Description

String LastIndexOf(Char, Int32, Int32) reports the zero-based index position of the last occurrence of the specified Unicode character in a substring 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(Char, Int32, Int32) has the following syntax.


public int LastIndexOf(
  char value,/*  w  ww.  ja va2s  .  c o m*/
  int startIndex,
  int count
)

Parameters

String.LastIndexOf(Char, Int32, 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.
  • count - The number of character positions to examine.

Returns

String.LastIndexOf(Char, Int32, 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, Int32)


//from   w  w w  .  j av  a  2  s  .c o  m
using System;

class Sample {
    public static void Main() {
        string str = "this is a test.";
        int start = str.Length-1;
        int at = 0;
        int count = 0;
        int end = start/2 - 1;
        while((start > -1) && (at > -1)){
            count = start - end; //Count must be within the substring.
            at = str.LastIndexOf('t', start, count);
            if (at > -1) {
                Console.Write("{0} ", at);
                start = at - 1;
            }
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version