C# String LastIndexOf(String, Int32, StringComparison)

Description

String LastIndexOf(String, Int32, StringComparison) reports the zero-based index of the last occurrence of a specified string within the current String object. The search starts at a specified character position and proceeds backward toward the beginning of the string. A parameter specifies the type of comparison to perform when searching for the specified string.

Syntax

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


public int LastIndexOf(
  string value,/*from   w  ww .ja va 2s. c o  m*/
  int startIndex,
  StringComparison comparisonType
)

Parameters

String.LastIndexOf(String, Int32, StringComparison) 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.
  • comparisonType - One of the enumeration values that specifies the rules for the search.

Returns

String.LastIndexOf(String, Int32, StringComparison) method returns The zero-based starting index position of the value parameter 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

In the following example, the LastIndexOf(String, Int32, StringComparison) method is used to find the position of a soft hyphen (U+00AD) followed by an "m", starting with the final "m" in two strings.


using System;/*  www. jav  a 2 s.  c om*/

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

      position = s1.LastIndexOf('m');
      if (position >= 0) {
         Console.WriteLine(s1.LastIndexOf(searchString, position, StringComparison.CurrentCulture));
         Console.WriteLine(s1.LastIndexOf(searchString, position, StringComparison.Ordinal));
      }
   }
}

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