In C#, LastIndexOfAny() works like IndexOfAny() but from the other end. It scans from right to left and returns the position of the last character that is in your char[] set. When nothing matches, it returns -1.
It is useful when the “end marker” can be one of several characters: a path that mixes \ and /, or a sentence that might end with ., !, or ?.
This is an instance member of System.String. Call it on a string variable, such as text.LastIndexOfAny(...). Calling it on a null variable throws NullReferenceException. It only reads the text. Strings are immutable, so the original string is never changed.
Syntax
Pass the set of characters. The returned index is still counted from the left.
C#
string text = "a-b_c-d";
char[] seps = { '-', '_' };
int last = text.LastIndexOfAny(seps); // 5
int before = text.LastIndexOfAny(seps, 4); // 3 (searches 4, 3, 2...)
Call
What it does
text.LastIndexOfAny(seps)
Last position of any character in seps.
text.LastIndexOfAny(seps, 4)
Start at index 4 and move backward.
text.LastIndexOfAny(seps, 4, 2)
Look at 2 characters only: index 4 and index 3.
Matching is ordinal and case-sensitive, like IndexOfAny. A null array throws ArgumentNullException. A startIndex outside the string throws ArgumentOutOfRangeException.
Library declarations
C#
public int LastIndexOfAny(char[] anyOf);
public int LastIndexOfAny(char[] anyOf, int startIndex);
public int LastIndexOfAny(char[] anyOf, int startIndex, int count);
Parameters
Parameter
Description
anyOf
Required. The characters to look for. Must not be null.
startIndex
Optional. Where the backward search begins.
count
Optional. How many characters to examine, moving toward the start.
Return Value
Return Value
Description
0 or more
Index of the last character that belongs to anyOf.
-1
None of the characters appear.
C# Examples
Example 1 — Find the Last Dash or Underscore
Compare the first and last separator in a name that mixes - and _.
C#
using System;
class Program {
static void Main() {
string text = "a-b_c-d";
char[] seps = { '-', '_' };
Console.WriteLine(text.IndexOfAny(seps));
Console.WriteLine(text.LastIndexOfAny(seps));
Console.WriteLine(text.Substring(text.LastIndexOfAny(seps) + 1));
}
}
Output
1
5
d
How It Works
Letters and separators alternate. Letters sit at even indexes, and the separators sit at 1, 3, and 5.
From the front.IndexOfAny stops at the first dash, index 1.
From the back.LastIndexOfAny meets the dash at 5 first, before the underscore at 3, so it returns 5.
The last piece. One step past that index is the final segment, d.
Example 2 — Get the File Name When Slashes Are Mixed
Paths copied between Windows and the web can mix \ and /. Search for both at once.
C#
using System;
class Program {
static void Main() {
string path = @"C:\data/logs\app.txt";
char[] slashes = { '\\', '/' };
int cut = path.LastIndexOfAny(slashes);
Console.WriteLine(cut);
Console.WriteLine(path.Substring(cut + 1));
}
}
Output
12
app.txt
How It Works
A verbatim string. The @ in front means each backslash is a normal character. In the char array, '\\' is how you write one backslash.
Both separators count. The path has backslashes at 2 and 12 and a forward slash at 7. Any of them is a match.
The last one wins. Scanning from the end reaches the backslash at 12 first.
Plain file name.Substring(13) returns app.txt. On its own, LastIndexOf('/') would have stopped at 7 and kept logs\app.txt.
Example 3 — Keep Only the Finished Sentences
A draft ends with half a sentence. Find the last ., !, or ? and split the text there.
C#
using System;
class Program {
static void Main() {
string draft = "Hi. Thanks! See you";
int end = draft.LastIndexOfAny(new[] { '.', '!', '?' });
Console.WriteLine(end);
Console.WriteLine(draft.Substring(0, end + 1));
Console.WriteLine(draft.Substring(end + 1).Trim());
}
}
Output
10
Hi. Thanks!
See you
How It Works
An inline set.new[] { '.', '!', '?' } creates the char[] right inside the call, since it is used only once.
The last end mark. The ! after Thanks sits at index 10. The dot at 2 is earlier, so it loses.
Keep the finished part.Substring(0, end + 1) takes 11 characters, which includes the ! itself.
The unfinished rest.Substring(11) starts with a space, so Trim() cleans it to See you.
Frequently Asked Questions
It returns the position of the last character in the string that is in your char array. The position is counted from the start. It returns -1 if none of the characters appear.
Call path.LastIndexOfAny(new[] { '\\', '/' }). It matches either separator and returns the last one.
No. It compares exact character codes. Add both upper and lower case letters to the array if you need both.
Backward, from startIndex toward index 0, just like LastIndexOf.
Use LastIndexOfAny when any of several single characters can be the marker. Use LastIndexOf when you look for one specific character or a whole word.