There are three methods you can use to find a character in a string:
- string.IndexOf(): Get the index of the first occurrence of a character.
- string.LastIndexOf(): Get the index of the last occurrence of a character.
- string.Contains(char): Check if the character exists or not.
I’ll show examples below.
Using string.IndexOf() to find a character
string.IndexOf() returns the index of the first occurrence of a character in a string. It searches the string from left to right, starting at the beginning. If it doesn’t find the character, it returns -1. Here’s an example:
var input = "hello world";
var index = input.IndexOf('o');
if (index > -1)
{
Console.WriteLine($"Found 'o' at index {index}");
}
Code language: C# (cs)
Notice that ‘o’ occurs twice in the string. It returned the first occurence of ‘o’ (i.e. “hello world”):
Found 'o' at index 4
Code language: plaintext (plaintext)
Case insensitive search with IndexOf()
To do a case insensitive search with IndexOf(), use the StringComparison overload. Here’s an example:
var input = "Hello World";
int index = input.IndexOf('w', StringComparison.CurrentCultureIgnoreCase);
if (index > -1)
{
Console.WriteLine($"Found 'w' at index {index}");
}
Code language: C# (cs)
Notice that it returned the index of the uppercased ‘W’ because it’s doing a case insensitive search:
Found 'w' at index 6
Code language: plaintext (plaintext)
Using string.LastIndexOf() to find a character
string.LastIndexOf() returns the index of the last occurrence of a character in a string. It searches the string from right to left, starting the end. In other words, it loops backwards. It returns -1 if it can’t find the character. Here’s an example:
var input = "hello world";
var index = input.LastIndexOf('o');
if (index > -1)
{
Console.WriteLine($"Found 'o' at index {index}");
}
Code language: C# (cs)
This outputs the following. It returned the index of the last occurrence of ‘o’ (i.e. “hello world”).
Found 'o' at index 7
Code language: plaintext (plaintext)
Case insensitive search with LastIndexOf()
To do a case insensitive search with LastIndexOf(), use the StringComparison overload. Oddly, there’s no overload that takes a char and StringComparison. Instead, you have to pass in the character as a string. Here’s an example:
var input = "HellO";
char findChar = 'o';
var index = input.LastIndexOf(findChar.ToString(), StringComparison.CurrentCultureIgnoreCase);
if (index > -1)
{
Console.WriteLine($"Found 'o' at index {index}");
}
Code language: C# (cs)
This outputs the following:
Found 'o' at index 4
Code language: plaintext (plaintext)
Using string.Contains(char) to check if a character exists
If you just need to know if a character exists in a string, and don’t need to know the position, use string.Contains(char). Here’s an example:
var input = "hello world";
if (input.Contains('o'))
{
Console.WriteLine("Found 'o'");
}
Code language: C# (cs)
Note: This method is available in .NET Core 2.1+.
This outputs:
Found 'o'
Code language: plaintext (plaintext)
Case insensitive search with Contains(char)
To do a case insensitive search with string.Contains(), use the StringComparison overload. Here’s an example:
var input = "HELLO!";
if (input.Contains('h', StringComparison.InvariantCultureIgnoreCase))
{
Console.WriteLine("Found 'h'");
}
Code language: C# (cs)
This outputs:
Found 'h'
Code language: plaintext (plaintext)