C# String LastIndexOfAny() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

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...)
CallWhat 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

ParameterDescription
anyOfRequired. The characters to look for. Must not be null.
startIndexOptional. Where the backward search begins.
countOptional. How many characters to examine, moving toward the start.

Return Value

Return ValueDescription
0 or moreIndex of the last character that belongs to anyOf.
-1None 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));
    }
}

How It Works

  1. Letters and separators alternate. Letters sit at even indexes, and the separators sit at 1, 3, and 5.
  2. From the front. IndexOfAny stops at the first dash, index 1.
  3. From the back. LastIndexOfAny meets the dash at 5 first, before the underscore at 3, so it returns 5.
  4. 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));
    }
}

How It Works

  1. A verbatim string. The @ in front means each backslash is a normal character. In the char array, '\\' is how you write one backslash.
  2. Both separators count. The path has backslashes at 2 and 12 and a forward slash at 7. Any of them is a match.
  3. The last one wins. Scanning from the end reaches the backslash at 12 first.
  4. 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());
    }
}

How It Works

  1. An inline set. new[] { '.', '!', '?' } creates the char[] right inside the call, since it is used only once.
  2. The last end mark. The ! after Thanks sits at index 10. The dot at 2 is earlier, so it loses.
  3. Keep the finished part. Substring(0, end + 1) takes 11 characters, which includes the ! itself.
  4. 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.

Next: Normalize()

Rewrite Unicode text into one standard form so that look-alike strings compare equal.

Normalize() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful