C# Tutorial

C# String

C# Array

C# Flow Control

C# Class and Object

C# Inheritance

C# Interface

C# Collection

C# Generic

C# File I/O

C# Delegate and Event

C# Exception

C# Process and Thread

C# ADO.NET Database Operations

C# IndexOf And LastIndexOf: Find Characters In A String

The IndexOf and LastIndexOf methods in C# allow you to find the position of a character or substring within a string. Both methods return the zero-based index of the first or last occurrence of the search value, or -1 if it's not found. In this tutorial, we'll explore how to use these methods effectively.

  • IndexOf

IndexOf can be used to find the first occurrence of a character or substring in a string. It has several overloaded versions:

int IndexOf(char value)
int IndexOf(char value, int startIndex)
int IndexOf(char value, int startIndex, int count)
int IndexOf(string value)
int IndexOf(string value, int startIndex)
int IndexOf(string value, int startIndex, int count)
int IndexOf(string value, StringComparison comparisonType)
int IndexOf(string value, int startIndex, StringComparison comparisonType)
int IndexOf(string value, int startIndex, int count, StringComparison comparisonType)

Example:

string sentence = "The quick brown fox jumps over the lazy dog.";

int indexOfFox = sentence.IndexOf("fox"); // 16
int indexOfThe = sentence.IndexOf("the"); // 31
int indexOfZ = sentence.IndexOf('z'); // 35
  • LastIndexOf

LastIndexOf can be used to find the last occurrence of a character or substring in a string. It has similar overloaded versions as IndexOf:

int LastIndexOf(char value)
int LastIndexOf(char value, int startIndex)
int LastIndexOf(char value, int startIndex, int count)
int LastIndexOf(string value)
int LastIndexOf(string value, int startIndex)
int LastIndexOf(string value, int startIndex, int count)
int LastIndexOf(string value, StringComparison comparisonType)
int LastIndexOf(string value, int startIndex, StringComparison comparisonType)
int LastIndexOf(string value, int startIndex, int count, StringComparison comparisonType)

Example:

string sentence = "The quick brown fox jumps over the lazy dog.";

int lastIndexOfThe = sentence.LastIndexOf("the"); // 31
int lastIndexOfO = sentence.LastIndexOf('o'); // 38
  • StringComparison enumeration

The StringComparison enumeration is used to specify the type of comparison to perform. The options include:

  • StringComparison.CurrentCulture
  • StringComparison.CurrentCultureIgnoreCase
  • StringComparison.InvariantCulture
  • StringComparison.InvariantCultureIgnoreCase
  • StringComparison.Ordinal
  • StringComparison.OrdinalIgnoreCase

Example:

string sentence = "The quick brown fox jumps over the lazy dog.";

int indexOfIgnoreCase = sentence.IndexOf("the", StringComparison.OrdinalIgnoreCase); // 0
int lastIndexOfIgnoreCase = sentence.LastIndexOf("the", StringComparison.OrdinalIgnoreCase); // 31

In this tutorial, we covered the basic usage of IndexOf and LastIndexOf methods in C#. These methods are useful for finding the position of characters or substrings within a string. Remember to explore the overloaded versions of these methods and the StringComparison enumeration to refine your search criteria.

  1. How to use IndexOf and LastIndexOf in C#

    The IndexOf and LastIndexOf methods are used to find the index of a character or substring in a string.

    string text = "C# programming";
    int indexOfCSharp = text.IndexOf("C#"); // Returns 0
    int lastIndexOfC = text.LastIndexOf("C"); // Returns 2
    
  2. Searching for characters in a string with IndexOf in C#

    Use IndexOf to find the first occurrence of a character in a string.

    string sentence = "Hello, C#";
    int indexOfComma = sentence.IndexOf(','); // Returns 5
    
  3. Finding the last occurrence with LastIndexOf in C#

    LastIndexOf returns the last occurrence of a character or substring in a string.

    string sentence = "C# is powerful, C# is versatile";
    int lastIndexOfCSharp = sentence.LastIndexOf("C#"); // Returns 19
    
  4. C# IndexOf and LastIndexOf case-insensitive search

    Perform case-insensitive search using IndexOf and LastIndexOf.

    string text = "C# is CaseSensitive, C# is CASEINSENSITIVE";
    int indexOfInsensitive = text.IndexOf("casesensitive", StringComparison.OrdinalIgnoreCase); // Returns 3
    int lastIndexOfInsensitive = text.LastIndexOf("casesensitive", StringComparison.OrdinalIgnoreCase); // Returns 23
    
  5. Searching for substrings in a string with IndexOf in C#

    Find the index of a substring within a string.

    string document = "C# programming guide";
    int indexOfProgramming = document.IndexOf("programming"); // Returns 3
    
  6. Index-based character retrieval with IndexOf in C#

    Use IndexOf to find the index of a specific character and retrieve it.

    string word = "C#";
    int indexOfC = word.IndexOf('C'); // Returns 0
    char firstChar = word[indexOfC]; // Retrieves 'C'
    
  7. Finding multiple occurrences with IndexOf in C#

    Iterate to find all occurrences of a character or substring.

    string text = "C# is cool, C# is powerful";
    int index = -1;
    while ((index = text.IndexOf("C#", index + 1)) != -1)
    {
        // Process each occurrence
    }
    
  8. C# IndexOf and LastIndexOf for character arrays

    Search for a character array within a string.

    string sentence = "C# programming";
    char[] charsToFind = { 'a', 'm' };
    int indexOfAM = sentence.IndexOfAny(charsToFind); // Returns 3
    
  9. Using IndexOf for pattern matching in C#

    Perform pattern matching using IndexOf.

    string text = "C# is awesome!";
    if (text.IndexOf("awesome") != -1)
    {
        // Pattern found
    }
    
  10. Handling not found scenarios with IndexOf in C#

    Check for -1 to handle scenarios where the substring is not found.

    string sentence = "Hello, World!";
    int index = sentence.IndexOf("C#"); // Returns -1 if not found
    
  11. Finding all occurrences with IndexOf in C#

    Use a loop to find all occurrences of a substring.

    string text = "C# is cool, C# is powerful";
    int index = -1;
    while ((index = text.IndexOf("C#", index + 1)) != -1)
    {
        // Process each occurrence
    }
    
  12. C# IndexOf and LastIndexOf for backward search

    Search backward using LastIndexOf.

    string text = "C# is cool, C# is powerful";
    int lastIndexOfCSharp = text.LastIndexOf("C#"); // Returns 19