Find a character in a string in C#
By FoxLearn 2/5/2025 9:05:19 AM 3
- string.IndexOf(): Returns the index of the first occurrence of a character.
- string.LastIndexOf(): Returns the index of the last occurrence of a character.
- string.Contains(char): Checks if a character exists in the string.
Let’s explore each method with examples.
Using string.IndexOf() to Find a Character
string.IndexOf()
returns the index of the first occurrence of a character in the string, searching from left to right. If the character is not found, it returns -1
.
var input = "programming is fun"; var index = input.IndexOf('g'); if (index > -1) { Console.WriteLine($"Found 'g' at index {index}"); }
In this example, the character 'g'
appears twice, and it returns the index of the first occurrence:
Output:
Found 'g' at index 3
Case-Insensitive Search with IndexOf()
To perform a case-insensitive search with IndexOf()
, use the StringComparison
overload.
var input = "CSharp Programming"; int index = input.IndexOf('p', StringComparison.CurrentCultureIgnoreCase); if (index > -1) { Console.WriteLine($"Found 'p' at index {index}"); }
This example returns the index of 'P'
despite the character being uppercase in the string.
Output:
Found 'p' at index 8
Using string.LastIndexOf() to Find a Character
string.LastIndexOf()
returns the index of the last occurrence of a character in a string, searching from right to left. If the character is not found, it returns -1
.
var input = "openAI is amazing"; var index = input.LastIndexOf('i'); if (index > -1) { Console.WriteLine($"Found 'i' at index {index}"); }
In this case, the last occurrence of 'i'
is returned:
Output:
Found 'i' at index 12
Case-Insensitive Search with LastIndexOf()
To perform a case-insensitive search with LastIndexOf()
, use the StringComparison
overload. You need to pass the character as a string when using StringComparison
.
var input = "Hello Universe"; char findChar = 'h'; var index = input.LastIndexOf(findChar.ToString(), StringComparison.CurrentCultureIgnoreCase); if (index > -1) { Console.WriteLine($"Found 'h' at index {index}"); }
This example searches for 'h'
case-insensitively.
Output:
Found 'h' at index 0
Using string.Contains() to Check if a Character Exists
If you simply want to know whether a character exists in the string, use string.Contains(char)
.
var input = "searching for a letter"; if (input.Contains('r')) { Console.WriteLine("Found 'r'"); }
This will check if 'r'
exists in the string.
Output:
Found 'r'
Case-Insensitive Search with Contains()
To search case-insensitively with string.Contains()
, you can use the StringComparison
overload.
var input = "EXAMPLE Text"; if (input.Contains('e', StringComparison.InvariantCultureIgnoreCase)) { Console.WriteLine("Found 'e'"); }
This performs a case-insensitive check for 'e'
.
Output:
Found 'e'
These are a few ways to find a character in a string in C#. The choice of method depends on whether you need the index of the character, the position of the last occurrence, or just a simple existence check.
- String to Byte Array Conversion in C#
- How to Trim a UTF-8 string to the specified number of bytes in C#
- How to Save a list of strings to a file in C#
- How to Convert string list to int list in C#
- How to Convert string list to float list in C#
- How to Remove a list of characters from a string in C#
- How to Check if a string contains any substring from a list in C#
- Remove non-alphanumeric characters from a string in C#