Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Strings and Text

C# String.IndexOf() method

Reference for C# String.IndexOf, LastIndexOf and IndexOfAny: find a char or substring, start positions, StringComparison, return values and pitfalls.

IndexOf searches a string for a character or a substring and returns the zero-based position of the first match, or -1 when there is none. LastIndexOf searches from the end, and IndexOfAny finds the first of several characters.

The returned index is the usual input for Substring and range slicing. When you only need a yes/no answer, use Contains.

C#
string path = "reports/2025/summary.txt";
Console.WriteLine(path.IndexOf('/'));       // 7
Console.WriteLine(path.LastIndexOf('/'));   // 12

Try it

C# Finding characters and substrings

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Case, empty strings and the -1 trap

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public int IndexOf(char value)
public int IndexOf(char value, int startIndex)
public int IndexOf(char value, int startIndex, int count)
public int IndexOf(string value)
public int IndexOf(string value, StringComparison comparisonType)
public int IndexOf(string value, int startIndex, StringComparison comparisonType)
public int LastIndexOf(char value)   // and the same overload set
public int IndexOfAny(char[] anyOf)
Name Description
value The character or substring to find.
startIndex Where the search begins (for LastIndexOf, where it begins going backward).
count How many characters to examine.
comparisonType Ordinal, OrdinalIgnoreCase, CurrentCulture, InvariantCulture and their ignore-case variants.
anyOf The set of characters for IndexOfAny.

Returns: the index of the first character of the match, or -1.

Errors: ArgumentNullException if value is null; ArgumentOutOfRangeException if startIndex is negative or larger than Length, or count goes past the end.

Usage notes

  • Check for -1. Passing -1 on to Substring throws. Test >= 0 first.
  • Strings default to culture-sensitive search. IndexOf(string) without a StringComparison uses the current culture, which can ignore zero-width characters and match differently from a byte-wise search; it is also slower. Pass StringComparison.Ordinal for identifiers, paths and protocol text. The char overloads are always ordinal.
  • Empty search string. IndexOf("") returns the start position (0 for the plain overload). This surprises people who build a "not found" check from user input.
  • Start index equal to Length is allowed and returns -1 (or Length for an empty search string).
  • LastIndexOf with a start index searches backward from that position toward the front.
  • Overlapping matches. To count all occurrences, advance by 1 (overlapping) or by the length of the match (non-overlapping), as the first example does.
  • Performance. Linear in the string length; the char and ordinal overloads use vectorized code. For repeated searches in the same text consider Regex or SearchValues<char>.
  • Spans. ReadOnlySpan<char>.IndexOf has the same behavior without allocating.