CompareInfo.IndexOf Method (String, Char)
Searches for the specified character and returns the zero-based index of the first occurrence within the entire source string.
Assembly: mscorlib (in mscorlib.dll)
abstract IndexOf : source:string * value:char -> int override IndexOf : source:string * value:char -> int
Parameters
- source
-
Type:
System.String
The string to search.
- value
-
Type:
System.Char
The character to locate within source.
Return Value
Type: System.Int32The zero-based index of the first occurrence of value, if found, within source; otherwise, -1. Returns 0 (zero) if value is an ignorable character.
| Exception | Condition |
|---|---|
| ArgumentNullException | source is null. |
This method searches the source string from the beginning of the string to the end of the string.
This overload performs a culture-sensitive search. If value represents a precomposed Unicode character, such as the ligature "Æ" (U+00C6), it might be considered equivalent to any occurrence of its components in the correct sequence, such as "AE" (U+0041, U+0045), depending on the culture. To perform an ordinal (culture-insensitive) search, where a character is considered equivalent to another character only if their Unicode code points are the same, you should call an overload that has a parameter of type CompareOptions and use the CompareOptions.Ordinal value. Unlike CompareInfo.IndexOf, which performs a culture-sensitive comparison, overloads of the String.IndexOf method that search for a character perform an ordinal comparison and overloads that search for a string perform a culture-sensitive comparison.
Note |
|---|
When possible, you should use string comparison methods that have a parameter of type CompareOptions to specify the kind of comparison expected. As a general rule, use linguistic options (using the current culture) for comparing strings displayed in the user interface and specify CompareOptions.Ordinal or CompareOptions.OrdinalIgnoreCase for security comparisons. |
Notes to Callers:
Character sets include ignorable characters, which are characters that are not considered when performing a linguistic or culture-sensitive sort. In a culture-sensitive search, if value is an ignorable character, the result is equivalent to searching with that character removed. In this case, the IndexOf(String, Char) method always returns 0 (zero) to indicate that the match is found at the beginning of source. In the following example, the IndexOf(String, Char) method is used to find the soft hyphen (U+00AD) in two strings. Only one of the strings contains a soft hyphen. In both cases, because the soft hyphen is an ignorable character, the method returns 0 (zero) to indicate that it has found a match at the beginning of the string.
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
