C# String LastIndexOf()

The LastIndexOf() method returns the index position of the last occurrence of a specified character or string within the given string.

Example

using System;  
namespace CsharpString {  
  class Test {
    public static void Main(string [] args) {
     
      string str = "Icecream";

// returns last index of 'c' int index = str.LastIndexOf('c');
Console.WriteLine(index); Console.ReadLine(); } } } // Output: 3

LastIndexOf() Syntax

The syntax of the string LastIndexOf() method is:

LastIndexOf(String value, int startIndex, int count, StringComparison comparisonType)

Here, LastIndexOf() is a method of class String.


LastIndexOf() Parameters

The LastIndexOf() method takes the following parameters:

  • value - substring to seek
  • startIndex - starting position of the search. The search proceeds from startIndex toward the beginning of the given string.
  • count - number of character positions to examine.
  • comparisonType - enumeration values that specifies the rules for the search

LastIndexOf() Return Value

  • returns the index of the last occurrence of the specified character/string
  • returns -1 if the specified character/string is not found.

Example 1: C# String LastIndexOf() With startIndex

using System;  
namespace CsharpString {  
  class Test {
    public static void Main(string [] args) {
     
      string str = "Icecream";

      // returns last index of 'c' 
int index = str.LastIndexOf('c', 2);
Console.WriteLine(index); Console.ReadLine(); } } }

Output

1

Notice the line,

int index = str.LastIndexOf('c', 2);

Here,

  • c - char to seek
  • 2 - starting index of search (search starts from index 2 towards the beginning of str)

Example 2: C# String LastIndexOf() With startIndex and count

using System;  
namespace CsharpString {  
  class Test {
    public static void Main(string [] args) {
     
      string str = "Icecream";

      int startIndex = 5;
      int count = 2;

// returns -1 as 'c' is not found int index = str.LastIndexOf('c', startIndex, count);
Console.WriteLine(index); Console.ReadLine(); } } }

Output

-1

Here,

int index = str.LastIndexOf('c', startIndex, count);

searches 2 characters ('e' and 'r') from index 5 towards the beginning of the str string.


Example 3: C# String LastIndexOf() With StringComparison

using System;  
namespace CsharpString {  
  class Test {
    public static void Main(string [] args) {
     
      string str = "icecream";

      int startIndex = 2;
      int count = 3;

      StringComparison comparisonType1 = StringComparison.CurrentCultureIgnoreCase;
      StringComparison comparisonType2 = StringComparison.CurrentCulture;

// ignores letter case int index1 = str.LastIndexOf("CE", startIndex, count, comparisonType1);
Console.WriteLine(index1);
// considers letter case int index2 = str.LastIndexOf("CE", startIndex, count, comparisonType2);
Console.WriteLine(index2); Console.ReadLine(); } } }

Output

1
-1

Here,

  • comparisonType1 - ignores letter case
  • comparisonType2 - considers letter case
Did you find this article helpful?