C# String IndexOf(String, Int32, StringComparison)
Description
String IndexOf(String, Int32, StringComparison)
reports
the zero-based index of the first occurrence of the specified string in the
current String object. Parameters specify the starting search position
in the current string and the type of search to use for the specified string.
Syntax
String.IndexOf(String, Int32, StringComparison)
has the following syntax.
public int IndexOf(
string value,//ww w . j av a 2s . co m
int startIndex,
StringComparison comparisonType
)
Parameters
String.IndexOf(String, Int32, StringComparison)
has the following parameters.
value
- The string to seek.startIndex
- The search starting position.comparisonType
- One of the enumeration values that specifies the rules for the search.
Returns
String.IndexOf(String, Int32, StringComparison)
method returns The zero-based index position of the value parameter if that string is found,
or -1 if it is not. If value is Empty, the return value is startIndex.
Example
In the following example, the IndexOf(String, Int32, StringComparison) method is used to find the position of a soft hyphen (U+00AD) followed by an "m" starting with the third character position in two strings.
//from w ww.jav a2s .c o m
using System;
public class Example
{
public static void Main()
{
string searchString = "\u00ADm";
string s1 = "ani\u00ADmal" ;
string s2 = "animal";
Console.WriteLine(s1.IndexOf(searchString, 2, StringComparison.CurrentCulture));
Console.WriteLine(s1.IndexOf(searchString, 2, StringComparison.Ordinal));
Console.WriteLine(s2.IndexOf(searchString, 2, StringComparison.CurrentCulture));
Console.WriteLine(s2.IndexOf(searchString, 2, StringComparison.Ordinal));
}
}
The code above generates the following result.