Home
C#
String LastIndexOf Examples
This page was last reviewed on Nov 21, 2024.
Dot Net Perls
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.
String IndexOf
This string method requires no explicit for-loop. We also have the LastIndexOfAny method, which searches for an array of strings.
for
Example. LastIndexOf() acts on instances of the string type. These can be referenced by the string keyword. There are many versions (overloads) of LastIndexOf.
Overload
Part 1 We can specify a char argument. LastIndexOf searches the instance string from the final character backwards to the first character.
Part 2 We can specify a string argument. If the value is located, its index is returned. Otherwise the special value -1 is returned.
Part 3 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"; // Part 1: find the last occurrence of "a." int index1 = value.LastIndexOf('a'); if (index1 != -1) { Console.WriteLine(index1); Console.WriteLine(value.Substring(index1)); } // Part 2: find the last occurrence of this string. int index2 = value.LastIndexOf("ab"); if (index2 != -1) { Console.WriteLine(index2); Console.WriteLine(value.Substring(index2)); } // Part 3: 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.
char Array
Here The example searches for the first instance of a "d" or "b" character starting from the end of the string.
using System; // Input string. const string value = "c_b_d_e"; // Search for last of any of these characters. int index = value.LastIndexOfAny(new char[] { 'd', 'b' }); Console.WriteLine(index);
4
Performance. StringComparison OrdinalIgnoreCase specifies we want to treat chars as number values, not culture-specific values. When optimizing for performance, always try an "ordinal" argument.
Also Consider storing the array in a local—and then use a reference to that cache as the argument. This will reduce object creations.
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
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.
This page was last updated on Nov 21, 2024 (simplify).
Home
Changes
© 2007-2024 Sam Allen.