In C#, LastIndexOf() searches a string from right to left and returns the position of the last match. The number it returns is still counted from the start of the string (index 0). If there is no match, it returns -1.
It is the tool for “the last one”: the last dot before a file extension, the last slash before a file name, or the latest entry in a log line. For the first match, use IndexOf().
This is an instance member of System.String. Call it on a string variable, such as text.LastIndexOf(...). Calling it on a null variable throws NullReferenceException. It only reads the text. Strings are immutable, so the original string is never changed.
Syntax
Same shape as IndexOf, but the search runs backward.
C#
string file = "report.final.pdf";
int last = file.LastIndexOf('.'); // 12
int first = file.IndexOf('.'); // 6
int word = file.LastIndexOf("final", StringComparison.Ordinal); // 7
Call
What it does
text.LastIndexOf('.')
Position of the last '.' (ordinal char match).
text.LastIndexOf("ab", StringComparison.Ordinal)
Position where the last "ab" starts.
text.LastIndexOf('.', 10)
Start at index 10 and search backward toward index 0.
text.LastIndexOf('.', 10, 4)
Look at 4 characters, from index 10 backward.
With a startIndex, the search goes from that index toward the beginning, not toward the end. As with IndexOf, the plain LastIndexOf(string) overload uses the current culture, so pass StringComparison.Ordinal for predictable results. The char overloads are always ordinal.
Library declarations
C#
public int LastIndexOf(char value);
public int LastIndexOf(char value, int startIndex);
public int LastIndexOf(char value, int startIndex, int count);
public int LastIndexOf(string value);
public int LastIndexOf(string value, int startIndex);
public int LastIndexOf(string value, int startIndex, int count);
public int LastIndexOf(string value, StringComparison comparisonType);
public int LastIndexOf(string value, int startIndex, StringComparison comparisonType);
public int LastIndexOf(string value, int startIndex, int count, StringComparison comparisonType);
Parameters
Parameter
Description
value
Required. The char or string to find. A null string throws ArgumentNullException.
startIndex
Optional. Where the backward search begins.
count
Optional. How many characters to examine, moving toward the start.
comparisonType
Optional. Case and culture rules, such as Ordinal.
Return Value
Return Value
Description
0 or more
Index (from the start) where the last match begins.
-1
No match.
C# Examples
Example 1 — Get a File Extension
A file name can hold several dots. Compare the first and the last one, and keep the text after the last.
C#
using System;
class Program {
static void Main() {
string path = "docs/2026/report.final.pdf";
int first = path.IndexOf('.');
int last = path.LastIndexOf('.');
Console.WriteLine("First dot: " + first);
Console.WriteLine("Last dot: " + last);
Console.WriteLine("Extension: " + path.Substring(last + 1));
}
}
Output
First dot: 16
Last dot: 22
Extension: pdf
How It Works
Two dots in one name.report.final.pdf has a dot after report and another before pdf.
The front search stops early.IndexOf finds the dot at 16, which would give the wrong extension, final.pdf.
The back search finds the right one.LastIndexOf reports 22, still counted from the left edge of the full path.
Keep what follows.Substring(last + 1) skips the dot itself and returns pdf.
Example 2 — Take the File Name from a Path
Cut a path at its last slash, and see why a missing slash still works with this pattern.
C#
using System;
class Program {
static void Main() {
string a = "docs/2026/report.pdf";
int slashA = a.LastIndexOf('/');
Console.WriteLine(slashA);
Console.WriteLine(a.Substring(slashA + 1));
string b = "readme.txt";
int slashB = b.LastIndexOf('/');
Console.WriteLine(slashB);
Console.WriteLine(b.Substring(slashB + 1));
}
}
Output
9
report.pdf
-1
readme.txt
How It Works
Last folder separator.docs ends at 3, the first slash is 4, 2026 fills 5 to 8, and the last slash is 9.
Everything after it.Substring(10) returns the plain file name report.pdf.
No slash at all.readme.txt has no folder, so the search returns -1.
A handy accident.-1 + 1 is 0, and Substring(0) is the whole string, so the same code works for both inputs. For real file paths, System.IO.Path.GetFileName also handles backslashes.
Example 3 — Find the Latest Entry in a Log Line
A log line repeats the word error. Find the first and last copies and keep only the newest message.
C#
using System;
class Program {
static void Main() {
string log = "error: disk; retry; error: timeout";
int first = log.IndexOf("error", StringComparison.Ordinal);
int last = log.LastIndexOf("error", StringComparison.Ordinal);
Console.WriteLine("First: " + first);
Console.WriteLine("Last: " + last);
Console.WriteLine("Latest message: " + log.Substring(last));
}
}
Output
First: 0
Last: 20
Latest message: error: timeout
How It Works
Search for a word. With a string argument, pass StringComparison.Ordinal so the result never depends on the machine's culture.
The first copy is at the start. The line begins with error, so IndexOf returns 0.
The last copy.LastIndexOf reports where the second errorbegins, index 20, not where it ends.
Keep the tail.Substring(20) returns the newest message. The original log string is still complete.
Frequently Asked Questions
The zero-based index where the last match starts, counted from the beginning of the string. It returns -1 when there is no match.
Find the last dot with name.LastIndexOf('.'), check that it is not -1, then use name.Substring(dot + 1). For real file paths, System.IO.Path.GetExtension does this for you.
Backward. LastIndexOf(value, startIndex) begins at startIndex and moves toward index 0. This is the opposite of IndexOf, which moves toward the end.
Yes. The char overloads compare exact codes. For strings, pass StringComparison.Ordinal for an exact match or StringComparison.OrdinalIgnoreCase to ignore case.
IndexOf returns the first match from the left. LastIndexOf returns the last match. Both return positions counted from the start of the string, and both return -1 when nothing matches.