Skip to main content This browser is no longer supported. Show
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support. String.IndexOf Method
DefinitionReports the zero-based index of the first occurrence of a specified Unicode character or string within this instance. The method returns -1 if the character or string is not found in this instance. In this articleOverloads
IndexOf(String, Int32, 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, the number of characters in the current string to search, and the type of search to use for the specified string.
Parametersvalue String The string to seek. startIndex Int32 The search starting position. count Int32 The number of character positions to examine. comparisonType StringComparison One of the enumeration values that specifies the rules for the search. ReturnsInt32The zero-based index position of the Exceptions
-or-
-or-
ExamplesThe following example demonstrates three overloads of the IndexOf method that find the first occurrence of a string within another string using different values of the StringComparison enumeration.
RemarksIndex numbering starts from 0 (zero). The The search begins at The Notes to CallersCharacter sets include ignorable characters, which are characters that are not considered when performing a linguistic or culture-sensitive comparison. In a culture-sensitive search (that is, if In the following example, the IndexOf(String, Int32, Int32, StringComparison) method is used to find the position of a soft hyphen (U+00AD) followed by an "m" starting in the third through sixth character positions in two strings. Only one of the strings contains the required substring. If the example is run on the .NET Framework 4 or later, in both cases, because the soft hyphen is an ignorable character, the method returns the index of "m" in the string when it performs a culture-sensitive comparison. When it performs an ordinal comparison, however, it finds the substring only in the first string. Note that in the case of the first string, which includes the soft hyphen followed by an "m", the method fails to return the index of the soft hyphen but instead returns the index of the "m" when it performs a culture-sensitive comparison. The method returns the index of the soft hyphen in the first string only when it performs an ordinal comparison.
Applies toIndexOf(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.
Parametersvalue String The string to seek. startIndex Int32 The search starting position. comparisonType StringComparison One of the enumeration values that specifies the rules for the search. ReturnsInt32The zero-based index position of the Exceptions
ExamplesThe following example demonstrates three overloads of the IndexOf method that find the first occurrence of a string within another string using different values of the StringComparison enumeration.
RemarksIndex numbering starts from 0. The The Notes to CallersCharacter sets include ignorable characters, which are characters that are not considered when performing a linguistic or culture-sensitive comparison. In a culture-sensitive search (that is, if 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. Only one of the strings contains the required substring. If the example is run on the .NET Framework 4 or later, in both cases, because the soft hyphen is an ignorable character, the method returns the index of "m" in the string when it performs a culture-sensitive comparison. Note that in the case of the first string, which includes the soft hyphen followed by an "m", the method fails to return the index of the soft hyphen but instead returns the index of the "m". The method returns the index of the soft hyphen in the first string only when it performs an ordinal comparison.
Applies toIndexOf(Char, Int32, Int32)Reports the zero-based index of the first occurrence of the specified character in this instance. The search starts at a specified character position and examines a specified number of character positions.
Parametersvalue Char A Unicode character to seek. startIndex Int32 The search starting position. count Int32 The number of character positions to examine. ReturnsInt32The zero-based index position of Exceptions
-or-
-or-
ExamplesThe following example demonstrates the IndexOf method.
RemarksThe search
begins at Index numbering starts from 0 (zero). The This method performs an ordinal (culture-insensitive) search, where a character is considered equivalent to another character only if their Unicode scalar values are the same. To perform a culture-sensitive search, use the CompareInfo.IndexOf method, where a Unicode scalar value representing a precomposed character, such as the ligature "Æ" (U+00C6), might be considered equivalent to any occurrence of the character's components in the correct sequence, such as "AE" (U+0041, U+0045), depending on the culture. See also
Applies toIndexOf(String, StringComparison)Reports the zero-based index of the first occurrence of the specified string in the current String object. A parameter specifies the type of search to use for the specified string.
Parametersvalue String The string to seek. comparisonType StringComparison One of the enumeration values that specifies the rules for the search. ReturnsInt32The index position of the ExceptionsExamplesThe following example demonstrates three overloads of the IndexOf method that find the first occurrence of a string within another string using different values of the StringComparison enumeration.
RemarksIndex numbering starts from zero. The Notes to CallersCharacter sets include ignorable characters, which are characters that are not considered when performing a linguistic or culture-sensitive comparison. In a culture-sensitive search (that is, if In the following example, the IndexOf(String, StringComparison) method is used to find three substrings (a soft hyphen (U+00AD), a soft hyphen followed by "n", and a soft hyphen followed by "m") in two strings. Only one of the strings contains a soft hyphen. If the example is run on the .NET Framework 4 or later, because the soft hyphen is an ignorable character, a culture-sensitive search returns the same value that it would return if the soft hyphen were not included in the search string. An ordinal search, however, successfully finds the soft hyphen in one string and reports that it is absent from the second string.
Applies toIndexOf(String, Int32, Int32)Reports the zero-based index of the first occurrence of the specified string in this instance. The search starts at a specified character position and examines a specified number of character positions.
Parametersvalue String The string to seek. startIndex Int32 The search starting position. count Int32 The number of character positions to examine. ReturnsInt32The zero-based index position of Exceptions
-or-
-or-
ExamplesThe following example finds the index of all occurrences of the string "he" within a substring of another string. Note that the number of characters to be searched must be recalculated for each search iteration.
RemarksIndex numbering starts from 0 (zero). The This method performs a word (case-sensitive and culture-sensitive) search using the current culture. The search begins at Character sets include ignorable characters, which are characters that are not considered when
performing a linguistic or culture-sensitive comparison. In a culture-sensitive search, if
Notes to CallersAs explained in Best Practices for Using Strings, we recommend that you avoid calling string comparison methods that substitute default values and instead call methods that require parameters to be explicitly specified. To use the comparison rules of the current culture to perform this operation, call the
IndexOf(String, Int32, Int32, StringComparison) method overload with a value of
CurrentCulture for its See also
Applies toIndexOf(Char, StringComparison)Reports the zero-based index of the first occurrence of the specified Unicode character in this string. A parameter specifies the type of search to use for the specified character.
Parametersvalue Char The character to seek. comparisonType StringComparison An enumeration value that specifies the rules for the search. ReturnsInt32The zero-based index of ExceptionsRemarksIndex numbering starts from zero. The Applies toIndexOf(Char, Int32)Reports the zero-based index of the first occurrence of the specified Unicode character in this string. The search starts at a specified character position.
Parametersvalue Char A Unicode character to seek. startIndex Int32 The search starting position. ReturnsInt32The zero-based index position of Exceptions
ExamplesThe following example demonstrates the IndexOf method.
RemarksIndex numbering starts from 0. The The search ranges from This method performs an ordinal (culture-insensitive) search, where a character is considered equivalent to another character only if their Unicode scalar values are the same. To perform a culture-sensitive search, use the CompareInfo.IndexOf method, where a Unicode scalar value representing a precomposed character, such as the ligature "Æ" (U+00C6), might be considered equivalent to any occurrence of the character's components in the correct sequence, such as "AE" (U+0041, U+0045), depending on the culture. See also
Applies toIndexOf(String)Reports the zero-based index of the first occurrence of the specified string in this instance.
Parametersvalue String The string to seek. ReturnsInt32The zero-based index position of ExceptionsExamplesThe following example searches for the "n" in "animal". Because string indexes begin at zero rather than one, the IndexOf(String) method indicates that the "n" is at position 1.
The following example uses the IndexOf method to determine the starting position of an animal name in a sentence. It then uses this position to insert an adjective that describes the animal into the sentence.
RemarksIndex numbering starts from zero. This method performs a word (case-sensitive and culture-sensitive) search using the current culture. The search begins at the first character position of this instance and continues until the last character position. Character sets include ignorable characters, which are
characters that are not considered when performing a linguistic or culture-sensitive comparison. In a culture-sensitive search, if
Notes to CallersAs explained in
Best Practices for Using Strings, we recommend that you avoid calling string comparison methods that substitute default values and instead call methods that require parameters to be explicitly specified. To find the first index of a substring within a string instance by using the comparison rules of the current culture, call the
IndexOf(String, StringComparison) method overload with a value of CurrentCulture for its See also
Applies toIndexOf(Char)Reports the zero-based index of the first occurrence of the specified Unicode character in this string.
Parametersvalue Char A Unicode character to seek. ReturnsInt32The zero-based index position of ExamplesThe following example demonstrates how you can search a String for a character using the IndexOf method.
RemarksIndex numbering starts from zero. This method performs an ordinal (culture-insensitive) search, where a character is considered equivalent to another character only if their Unicode scalar values are the same. To perform a culture-sensitive search, use the CompareInfo.IndexOf method, where a Unicode scalar value representing a precomposed character, such as the ligature "Æ" (U+00C6), might be considered equivalent to any occurrence of the character's components in the correct sequence, such as "AE" (U+0041, U+0045), depending on the culture. See also
Applies toIndexOf(String, Int32)Reports the zero-based index of the first occurrence of the specified string in this instance. The search starts at a specified character position.
Parametersvalue String The string to seek. startIndex Int32 The search starting position. ReturnsInt32The zero-based index position of Exceptions
ExamplesThe following example searches for all occurrences of a specified string within a target string.
RemarksIndex numbering starts from 0. The This method performs a word
(case-sensitive and culture-sensitive) search using the current culture. The search begins at the Character sets include ignorable characters, which are characters that are not considered when performing a linguistic or culture-sensitive comparison. In a culture-sensitive search, if
Notes to CallersAs explained in Best Practices for Using Strings, we recommend that you
avoid calling string comparison methods that substitute default values and instead call methods that require parameters to be explicitly specified. To find the first index of a substring that occurs after a particular character position by using the comparison rules of the current culture, call the
IndexOf(String, Int32, StringComparison) method overload with a value of CurrentCulture for its See also
Applies toWhich method call gives the position of the first occurrence of x in string s1?indexOf("X")
What is use of charAt () method Mcq?Explanation: charAt() return one character only not array of character.
Which of the following methods will create string in Java Mcq?The intern() and toString() methods are of String class. Hence, the correct answer is option (c).
How do you get the index of the first occurrence of character in string hello how are you?The indexOf() method returns the position of the first occurrence of specified character(s) in a string. Tip: Use the lastIndexOf method to return the position of the last occurrence of specified character(s) in a string.
|