LastIndexOf. This C# 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.
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.
Tip LastIndexOf internally searches the instance string from the final character backwards to the first character.
Return 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.
Finally We use LastIndexOf to do a case-insensitive search. This option is only available with the string overloads of LastIndexOf.
using System;
// The string we are searching.
string value = "abc abc";
// Find the last occurrence of "a."
int index1 = value.LastIndexOf('a');
if (index1 != -1)
{
Console.WriteLine(index1);
Console.WriteLine(value.Substring(index1));
}
// Find the last occurrence of this string.
int index2 = value.LastIndexOf("ab");
if (index2 != -1)
{
Console.WriteLine(index2);
Console.WriteLine(value.Substring(index2));
}
// Find the last occurrence of this string, ignoring the case.
int index3 = value.LastIndexOf("BC", StringComparison.OrdinalIgnoreCase);
if (index3 != -1)
{
Console.WriteLine(index3);
Console.WriteLine(value.Substring(index3));
}4
abc
4
abc
5
bc
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.
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.
using System;
// 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);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.
Discussion. The IndexOf methods are similar to the LastIndexOf method. They search from the first positions forward, not the last positions backwards.
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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.