C# LastIndexOf

String type

LastIndexOf searches strings in reverse. You want to find the location of the last occurrence of a letter or substring in a C# string. The LastIndexOf method declaratively locates the last position of the character or string—without a for-loop.

This C# example program demonstrates the LastIndexOf string method.

Example

Note

We see that the LastIndexOf method acts on instances of the string type, which can be referenced by the 'string' keyword or the actual 'String' uppercased class in the base class library. There are nine versions of the LastIndexOf method on the string type, and they contain different implementations but logically are similar.

The method internally searches the instance string from the final character backwards to the first character. If the specified value parameter is located, its index is returned, and otherwise the special value -1 is returned.

Program that uses LastIndexOf method [C#]

using System;

class Program
{
    static void Main()
    {
	//
	// The string we are searching.
	string value = "Dot Net Perls";
	//
	// Find the last occurrence of N.
	int index1 = value.LastIndexOf('N');
	if (index1 != -1)
	{
	    Console.WriteLine(index1);
	    Console.WriteLine(value.Substring(index1));
	}
	//
	// Find the last occurrence of this string.
	int index2 = value.LastIndexOf("Perls");
	if (index2 != -1)
	{
	    Console.WriteLine(index2);
	    Console.WriteLine(value.Substring(index2));
	}
	//
	// Find the last 'e'.
	// ... This will not find the first 'e'.
	int index3 = value.LastIndexOf('e');
	if (index3 != -1)
	{
	    Console.WriteLine(index3);
	    Console.WriteLine(value.Substring(index3));
	}
	//
	// Find the last occurrence of this string in any case.
	int index4 = value.LastIndexOf("PERL", StringComparison.OrdinalIgnoreCase);
	if (index4 != -1)
	{
	    Console.WriteLine(index4);
	    Console.WriteLine(value.Substring(index4));
	}
    }
}

Output

4
Net Perls
8
Perls
9
erls
8
Perls
Note

Description. In Visual Studio you can select overloads of the best LastIndexOf method by typing the period and scrolling through the IntelliSense popup. There are LastIndexOf overloads that operate on a startIndex and proceed through a certain number of characters. In the method calls here, the starting index is the final character index and the count is the string length.

Tip: When you specify the startIndex, specify the position where searching begins, not simply the first character in the range you are searching.

Case-insensitive string searching. The example program also demonstrates how you can use the LastIndexOf method to do a case-insensitive string search. This option is only available with the string type overloads of the LastIndexOf method. The StringComparison.OrdinalIgnoreCase value is an enumerated constant that specifies that you want to treat characters as number values (ordinals) and not culture-specific values.

Note: Ordinal searches perform considerably faster than culture-variant options.

Methods

.NET Framework information

There are other IndexOf, IndexOfAny, and LastIndexOfAny methods available in the .NET Framework targeting the C# programming language. The IndexOf methods are the same as the LastIndexOf method except they search from the first positions forward, not the last positions backwards. The IndexOf string methods are used more commonly in C# programs.

IndexOf IndexOfAny LastIndexOfAny

Summary

The C# programming language

We looked at the LastIndexOf method and its usage in the C# programming language. We saw that it can be used with the string type parameters; how it can be used with the char type parameter; an example of its usage with the StringComparison. OrdinalIgnoreCase enumerated constant to provide case-insensitivity; and how it relates to other string searching methods.

String Type
.NET