Summary: in this tutorial, you will learn how to use the C# String IndexOf()
method to return the position of the first occurrence of a specified string within the current string.
Introduction to the C# String IndexOf() method
The String IndexOf()
method allows you to check whether a string is present in the current string:
public int IndexOf (string value);
Code language: C# (cs)
The IndexOf()
method accepts a parameter value
that is a string to search for. It returns the position of the first occurrence of the string value
in the current string.
If the string value
is not present in the current string, the IndexOf()
method returns -1
.
To determine if a string exists within another string without retrieving its position, you can use the Contains()
method instead.
The following example uses the IndexOf()
method to get the starting position of the string "is"
in the string "C# is awesome"
:
using static System.Console;
var message = "C# is awesome";
var index = message.IndexOf("is");
WriteLine(index);
Code language: C# (cs)
Output:
3
Code language: C# (cs)
By default, the IndexOf()
method uses the ordinal comparison which considers both case sensitivity and culture insensitivity for string comparison.
For example, the following program outputs -1
because the string "IS"
which is the uppercase of the string "is"
, is not present in the string "C# is Awesome"
:
using static System.Console;
var message = "C# is awesome";
var index = message.IndexOf("IS");
WriteLine(index);
Code language: C# (cs)
Output:
-1
Code language: C# (cs)
To compare strings case-sensitively, you use the following overload of the IndexOf()
method:
public int IndexOf (
string value,
StringComparison comparisonType
);
Code language: C# (cs)
In this method, the comparisonType
is one of the values of the StringComparison
enum.
For example, the following uses the StringComparison.OrdinalIgnoreCase
that ignores cases of the strings when comparing them:
using static System.Console;
var message = "C# is awesome";
var index = message.IndexOf("IS", StringComparison.OrdinalIgnoreCase);
WriteLine(index);
Code language: C# (cs)
Output:
3
Code language: C# (cs)
By default, the IndexOf()
method starts searching for a substring from the beginning of a string. To start searching for a substring at a specified character position, you use the following overload of the IndexOf()
method:
public int IndexOf (
char value,
int startIndex
);
Code language: C# (cs)
In this syntax, the startIndex
specifies the starting index to search for the string value in the current string.
For example, the following use the IndexOf()
method to find the index of the second position of the string "se"
in the string "She sees the sea"
:
using static System.Console;
var message = "She sees the sea";
var index = message.IndexOf("se");
if(index != -1)
{
var secondIndex = message.IndexOf("se", index + 1);
WriteLine(secondIndex);
}
Code language: C# (cs)
Output:
13
Code language: C# (cs)
To search for a string within a substring at a specified index within a number of character positions, you use the following overload of the IndexOf() method:
public int IndexOf (
string value,
int startIndex,
int count
);
Code language: C# (cs)
In this syntax, the count
is the number of characters positions to search.
The following example uses the count
parameter to search for the second occurrence of the string "se"
in the string "She sees the sea"
within 5 character positions:
using static System.Console;
var message = "She sees the sea";
var index = message.IndexOf("se");
if (index != -1)
{
var secondIndex = message.IndexOf("se", index + 1, 5);
WriteLine(secondIndex);
}
Code language: C# (cs)
Output:
-1
The program output -1
because within 5 character positions from the index+1
, there is no occurrence of the string "se"
in the searched string.
Summary
- Use C# String
IndexOf()
method to find the zero-based index of the first occurrence of a string within the current string.