C# String LastIndexOf

Summary: in this tutorial, you will learn how to use the C# String LastIndexOf() method to return the position of the last occurrence of a specified string within the current string.

Introduction to the C# String LastIndexOf()() method

The String LastIndexOf() method allows you to check whether a string is present in the current string:

public int LastIndexOf(string value);Code language: C# (cs)

The LastIndexOf() accepts a parameter value that is a string to search for. It returns the position of the last occurrence of the string value in the current string.

If the string value is not present in the current string, the LastIndexOf()() method returns -1.

To determine if a string exists within another string without retrieving its position, you can use the Contains() method instead.

The following example uses the LastIndexOf() method to get the index of the string "is" in the string "C# is awesome":

using static System.Console;

var message = "He sees the sea";
var index = message.LastIndexOf("se");

WriteLine(index);Code language: C# (cs)

Output:

12Code language: C# (cs)

By default, the LastIndexOf() method uses the ordinal comparison which considers both case sensitivity and culture insensitivity for string comparison.

For example, the following outputs -1 because the string "SE" is not present in the string "He sees the sea":

using static System.Console;

var message = "He sees the sea";
var index = message.LastIndexOf("SE");

WriteLine(index);Code language: C# (cs)

Output:

-1Code language: C# (cs)

To compare strings case-sensitively, you use the following overload of the LastIndexOf() method:

public int LastIndexOf() (
   string value, 
   StringComparison comparisonType
);Code language: C# (cs)

In this method, the comparisonType is one of the values of the StringComparison enum.

For example, the following uses the StringComparison.OrdinalIgnoreCase that ignores cases of the strings when comparing them:

using static System.Console;

var message = "He sees the sea";

var index = message.LastIndexOf(
    "SE",
    StringComparison.OrdinalIgnoreCase
);

WriteLine(index);Code language: C# (cs)

Output:

12Code language: C# (cs)

To start searching for a substring from the end of a string at a specified character position (startIndex) and proceeds backward toward the beginning of the string, you use the following overload of the LastIndexOf() method:

public int LastIndexOf() (
   char value, 
   int startIndex
);Code language: C# (cs)

In this syntax, the startIndex specifies the starting index to search for the string value in the current string.

For example, the following use the LastIndexOf() method to find the index of the second occurrence of the string "se" from the end of the string "She sees the sea":

using static System.Console;

var message = "She sees the sea";
var index = message.LastIndexOf("se");

if (index != -1)
{
    var secondIndex = message.LastIndexOf("se", index - 1);
    WriteLine(secondIndex);
}Code language: C# (cs)

Output:

4Code language: C# (cs)

To start searching for a substring value at a specified index startIndex within a number of character positions count from the end of a string, you use the following overload of the LastIndexOf() method:

public int LastIndexOf() (
    string value, 
    int startIndex, 
    int count
);Code language: C# (cs)

The following example uses the count parameter to search for the second occurrence of the string "se" from the end of the string "She sees the sea" within 5 character positions:

using static System.Console;

var message = "She sees the sea";
var index = message.LastIndexOf("se");

if (index != -1)
{
    var secondIndex = message.LastIndexOf("se", index - 1, 5);
    WriteLine(secondIndex);
}Code language: C# (cs)

It returns -1 because within 5 character positions from the index-1, there is no occurrence of the string "se" in the searched string.

Summary

  • Use C# String LastIndexOf() method to find the zero-based index of the last occurrence of a string within another string.
Was this tutorial helpful ?