0
0
CsharpHow-ToBeginner · 3 min read

How to Use IndexOf in C#: Syntax and Examples

In C#, use IndexOf to find the position of a character or substring inside a string. It returns the zero-based index of the first occurrence or -1 if not found. For example, string.IndexOf("a") returns the position of 'a' in the string.
📐

Syntax

The IndexOf method is called on a string to find the position of a character or substring.

  • string.IndexOf(char value): Finds the first occurrence of a character.
  • string.IndexOf(string value): Finds the first occurrence of a substring.
  • Returns an int representing the zero-based index or -1 if not found.
csharp
int index = myString.IndexOf('a');
int substringIndex = myString.IndexOf("hello");
💻

Example

This example shows how to find the position of a character and a substring in a string using IndexOf. It prints the index or -1 if the item is not found.

csharp
using System;

class Program
{
    static void Main()
    {
        string text = "Hello, world!";

        int charIndex = text.IndexOf('o');
        Console.WriteLine("Index of 'o': " + charIndex);

        int substringIndex = text.IndexOf("world");
        Console.WriteLine("Index of \"world\": " + substringIndex);

        int notFoundIndex = text.IndexOf('z');
        Console.WriteLine("Index of 'z': " + notFoundIndex);
    }
}
Output
Index of 'o': 4 Index of "world": 7 Index of 'z': -1
⚠️

Common Pitfalls

Common mistakes when using IndexOf include:

  • Not checking if the result is -1 before using the index, which can cause errors.
  • Confusing zero-based index with one-based positions.
  • Using IndexOf without considering case sensitivity (it is case-sensitive by default).

To avoid errors, always check if the returned index is -1 before using it.

csharp
string text = "Hello";

// Wrong: Using index without checking
int index = text.IndexOf('z');
// Using index directly may cause errors if -1
// Correct:
if (index != -1)
{
    Console.WriteLine("Found at " + index);
}
else
{
    Console.WriteLine("Not found");
}
📊

Quick Reference

IndexOf returns the zero-based index of the first match or -1 if not found.

  • Case-sensitive search by default.
  • Overloads allow specifying start index and comparison type.
  • Use IndexOf to check if a substring exists by testing if result is not -1.
MethodDescriptionReturns
IndexOf(char value)Finds first occurrence of a characterZero-based index or -1
IndexOf(string value)Finds first occurrence of a substringZero-based index or -1
IndexOf(char value, int startIndex)Starts search from a specific indexZero-based index or -1
IndexOf(string value, StringComparison)Search with case optionsZero-based index or -1

Key Takeaways

Use IndexOf to find the position of a character or substring in a string.
IndexOf returns -1 if the item is not found, so always check before using the index.
IndexOf is case-sensitive by default; use overloads for case-insensitive search.
The returned index is zero-based, meaning counting starts at 0.
You can specify a start position or comparison rules with different overloads.