C# LastIndexOf Examples

Call the LastIndexOf and LastIndexOfAny string methods to locate characters from the right.

LastIndexOf. This method searches strings from the right. It finds the location of the last occurrence of a letter or substring. It is the reversed version of IndexOf.StringsIndexOf

A key advantage. LastIndexOf requires no explicit for-loop. We also have the LastIndexOfAny method. It searches for an array of strings.For

An example. LastIndexOf() acts on instances of the string type. These can be referenced by the string keyword. There are 9 versions (overloads) of LastIndexOf.Overload
Tip: LastIndexOf internally searches the instance string from the final character backwards to the first character.
Returns: If the specified value parameter is located, its index is returned. Otherwise the special value -1 is returned.
Start: When we specify the start index, specify the position where searching begins, not the first character in the range.
Insensitive: We use LastIndexOf to do a case-insensitive search. This option is only available with the string overloads of LastIndexOf.
C# program that uses LastIndexOf method 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, ignoring the 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

LastIndexOfAny. This is a separate method. It searches for multiple characters in reverse. It returns the final position of any of a set of characters.
Tip: When we call LastIndexOfAny, we must provide a character array. This can be created directly in the argument slot.
Char Array
Here: The example searches for the first instance of a "d" or "b" character starting from the end of the string.
And: The position returned is 5, which is the final "d" in the input string. The search terminates when something is found.
C# program that uses LastIndexOfAny method using System; class Program { static void Main() { // Input string. const string value = "ccbbddee"; // Search for last of any of these characters. int index = value.LastIndexOfAny(new char[] { 'd', 'b' }); Console.WriteLine(index); } } Output 5

Notes, Visual Studio. In Visual Studio we can select overloads of LastIndexOf by typing the period after the string. Then scroll through the IntelliSense window.

Notes, ordinal. StringComparison.OrdinalIgnoreCase specifies we want to treat characters as number values (ordinals) and not culture-specific values. Ordinal searches are faster.
Tip: When optimizing for performance, always try an "ordinal" argument. But make sure to have a correct program first.

Cache, LastIndexOfAny. The performance of LastIndexOfAny is not ideal. Consider storing the array in a local—and then use a reference to that cache as the argument.
Note: This will avoid an object creation on every call. Reducing object creations reduces memory pressure and garbage collections.

A discussion. The IndexOf methods are similar to the LastIndexOf method. They search from the first positions forward, not the last positions backwards.

A summary. LastIndexOf and LastIndexOfAny locate strings from the right side. LastIndexOf can be used with a string argument. But it can be called with a char—this influences performance.Char

© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.