C# String LastIndexOf(String, Int32)

Description

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

Syntax

String.LastIndexOf(String, Int32) has the following syntax.


public int LastIndexOf(
  string value,
  int startIndex
)

Parameters

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

Returns

String.LastIndexOf(String, 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 String.Empty, the return value is the smaller of startIndex and the last index position in this instance.

Example


using System;//from   www . ja v a 2 s.  com

public class Example
{
   public static void Main()
   {
      int position = 0;
      string s1 = "ani\u00ADmalani\u00ADmalani\u00ADmal";

      // Find the index of the soft hyphen.
      position = s1.LastIndexOf("m",1);
      Console.WriteLine("'m' at position {0}", position);
      if (position >= 0)
         Console.WriteLine(s1.LastIndexOf("\u00AD", position));

   }
}

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