Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
IndexOf Method (String, Char, Int32, CompareOptions)

CompareInfo.IndexOf Method (String, Char, Int32, CompareOptions)

Searches for the specified character and returns the zero-based index of the first occurrence within the section of the source string that extends from the specified index to the end of the string using the specified CompareOptions value.

Namespace:  System.Globalization
Assemblies:   System.Globalization (in System.Globalization.dll)
  mscorlib (in mscorlib.dll)

'Declaration
Public Overridable Function IndexOf ( _
	source As String, _
	value As Char, _
	startIndex As Integer, _
	options As CompareOptions _
) As Integer

Parameters

source
Type: System.String

The string to search.

value
Type: System.Char

The character to locate within source.

startIndex
Type: System.Int32

The zero-based starting index of the search.

options
Type: System.Globalization.CompareOptions

A value that defines how source and value should be compared. options is either the enumeration value Ordinal, or a bitwise combination of one or more of the following values: IgnoreCase, IgnoreSymbols, IgnoreNonSpace, IgnoreWidth, and IgnoreKanaType.

Return Value

Type: System.Int32
The zero-based index of the first occurrence of value, if found, within the section of source that extends from startIndex to the end of source, using the specified comparison options; otherwise, -1. Returns startIndex if value is an ignorable character.

ExceptionCondition
ArgumentNullException

source is Nothing.

ArgumentOutOfRangeException

startIndex is outside the range of valid indexes for source.

ArgumentException

options contains an invalid CompareOptions value.

The source string is searched forward starting at startIndex and ending at the end of the string.

The CompareOptions.StringSort value is not valid for this method.

If options does not include the Ordinal value, this overload performs a culture-sensitive search. If the character is a Unicode value representing a precomposed 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. If options includes the Ordinal value, this overload performs an ordinal (culture-insensitive) search. A character is considered equivalent to another character only if the Unicode values are the same. Overloads of String.IndexOf that search for a character perform an ordinal search, whereas those that search for a string perform a culture-sensitive search.

NoteNote

When possible, you should call 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, Int32, CompareOptions) method always returns startIndex, the character position at which the search first began. In the following example, the IndexOf(String, Char, Int32, CompareOptions) method is used to find the soft hyphen (U+00AD) after an "n" in two strings. Only one of the strings contains a soft hyphen. In both cases, because the soft hyphen is an ignorable character, a culture-sensitive search returns 1 to indicate that it has found a match at the position of the "n". An ordinal search, however, successfully finds the soft hyphen in one string and reports that it is absent from the second string.

Imports System.Globalization

Module Example
   Public Sub Main()
      Dim ci As CompareInfo = CultureInfo.CurrentCulture.CompareInfo

      Dim softHyphen As Char = ChrW(&h00AD)
      Dim position As Integer 

      Dim s1 As String = "ani" + softHyphen + "mal" 
      Dim s2 As String = "animal" 

      ' Find the index of the soft hyphen using culture-sensitive comparison.
      position = ci.IndexOf(s1, "n"c)
      Console.WriteLine("'n' at position {0}", position)
      If position >= 0 Then
         Console.WriteLine(ci.IndexOf(s1, softHyphen, position, CompareOptions.IgnoreCase))
      End If

      position = ci.IndexOf(s2, "n"c)
      Console.WriteLine("'n' at position {0}", position)
      If position >= 0 Then
         Console.WriteLine(ci.IndexOf(s2, softHyphen, position, CompareOptions.IgnoreCase))
      End If   

      ' Find the index of the soft hyphen using ordinal comparison.
      position = ci.IndexOf(s1, "n"c)
      Console.WriteLine("'n' at position {0}", position)
      If position >= 0 Then
         Console.WriteLine(ci.IndexOf(s1, softHyphen, position, CompareOptions.Ordinal))
      End If

      position = ci.IndexOf(s2, "n"c)
      Console.WriteLine("'n' at position {0}", position)
      If position >= 0 Then
         Console.WriteLine(ci.IndexOf(s2, softHyphen, position, CompareOptions.Ordinal))
      End If    
   End Sub 
End Module 
' The example displays the following output: 
'       'n' at position 1 
'       1 
'       'n' at position 1 
'       1 
'       'n' at position 1 
'       3 
'       'n' at position 1 
'       -1

The following example determines the indexes of the first and last occurrences of a character or a substring within a portion of a string. Note that IndexOf and LastIndexOf are searching in different portions of the string, even with the same startIndex parameter.

Imports System
Imports System.Globalization
Imports Microsoft.VisualBasic

Public Class SamplesCompareInfo

   Public Shared Sub Main()

      ' Creates CompareInfo for the InvariantCulture. 
      Dim myComp As CompareInfo = CultureInfo.InvariantCulture.CompareInfo

      ' iS is the starting index of the substring. 
      Dim [iS] As Integer = 20
      ' myT1 is the string used for padding. 
      Dim myT1 As [String]

      ' Searches for the ligature Æ. 
      Dim myStr As [String] = "Is AE or ae the same as Æ or æ?"
      Console.WriteLine()

      myT1 = New [String]("-"c, [iS])
      Console.WriteLine("IndexOf( String, *, {0}, * )", [iS])
      Console.WriteLine("Original      : {0}", myStr)
      Console.WriteLine("No options    : {0}{1}", myT1, myStr.Substring([iS]))
      PrintMarker("           AE : ", myComp.IndexOf(myStr, "AE", [iS]), - 1)
      PrintMarker("           ae : ", myComp.IndexOf(myStr, "ae", [iS]), - 1)
      PrintMarker("            Æ : ", myComp.IndexOf(myStr, "Æ"c, [iS]), - 1)
      PrintMarker("            æ : ", myComp.IndexOf(myStr, "æ"c, [iS]), - 1)
      Console.WriteLine("Ordinal       : {0}{1}", myT1, myStr.Substring([iS]))
      PrintMarker("           AE : ", myComp.IndexOf(myStr, "AE", [iS], CompareOptions.Ordinal), - 1)
      PrintMarker("           ae : ", myComp.IndexOf(myStr, "ae", [iS], CompareOptions.Ordinal), - 1)
      PrintMarker("            Æ : ", myComp.IndexOf(myStr, "Æ"c, [iS], CompareOptions.Ordinal), - 1)
      PrintMarker("            æ : ", myComp.IndexOf(myStr, "æ"c, [iS], CompareOptions.Ordinal), - 1)
      Console.WriteLine("IgnoreCase    : {0}{1}", myT1, myStr.Substring([iS]))
      PrintMarker("           AE : ", myComp.IndexOf(myStr, "AE", [iS], CompareOptions.IgnoreCase), - 1)
      PrintMarker("           ae : ", myComp.IndexOf(myStr, "ae", [iS], CompareOptions.IgnoreCase), - 1)
      PrintMarker("            Æ : ", myComp.IndexOf(myStr, "Æ"c, [iS], CompareOptions.IgnoreCase), - 1)
      PrintMarker("            æ : ", myComp.IndexOf(myStr, "æ"c, [iS], CompareOptions.IgnoreCase), - 1)

      myT1 = New [String]("-"c, myStr.Length - [iS] - 1)
      Console.WriteLine("LastIndexOf( String, *, {0}, * )", [iS])
      Console.WriteLine("Original      : {0}", myStr)
      Console.WriteLine("No options    : {0}{1}", myStr.Substring(0, [iS] + 1), myT1)
      PrintMarker("           AE : ", - 1, myComp.LastIndexOf(myStr, "AE", [iS]))
      PrintMarker("           ae : ", - 1, myComp.LastIndexOf(myStr, "ae", [iS]))
      PrintMarker("            Æ : ", - 1, myComp.LastIndexOf(myStr, "Æ"c, [iS]))
      PrintMarker("            æ : ", - 1, myComp.LastIndexOf(myStr, "æ"c, [iS]))
      Console.WriteLine("Ordinal       : {0}{1}", myStr.Substring(0, [iS] + 1), myT1)
      PrintMarker("           AE : ", - 1, myComp.LastIndexOf(myStr, "AE", [iS], CompareOptions.Ordinal))
      PrintMarker("           ae : ", - 1, myComp.LastIndexOf(myStr, "ae", [iS], CompareOptions.Ordinal))
      PrintMarker("            Æ : ", - 1, myComp.LastIndexOf(myStr, "Æ"c, [iS], CompareOptions.Ordinal))
      PrintMarker("            æ : ", - 1, myComp.LastIndexOf(myStr, "æ"c, [iS], CompareOptions.Ordinal))
      Console.WriteLine("IgnoreCase    : {0}{1}", myStr.Substring(0, [iS] + 1), myT1)
      PrintMarker("           AE : ", - 1, myComp.LastIndexOf(myStr, "AE", [iS], CompareOptions.IgnoreCase))
      PrintMarker("           ae : ", - 1, myComp.LastIndexOf(myStr, "ae", [iS], CompareOptions.IgnoreCase))
      PrintMarker("            Æ : ", - 1, myComp.LastIndexOf(myStr, "Æ"c, [iS], CompareOptions.IgnoreCase))
      PrintMarker("            æ : ", - 1, myComp.LastIndexOf(myStr, "æ"c, [iS], CompareOptions.IgnoreCase))

      ' Searches for the combining character sequence Latin capital letter U with diaeresis or Latin small letter u with diaeresis.
      myStr = "Is " & ChrW(&H0055) & ChrW(&H0308) & " or " & ChrW(&H0075) & ChrW(&H0308) & " the same as " & ChrW(&H00DC) & " or " & ChrW(&H00FC) & "?"
      Console.WriteLine()

      myT1 = New [String]("-"c, [iS])
      Console.WriteLine("IndexOf( String, *, {0}, * )", [iS])
      Console.WriteLine("Original      : {0}", myStr)
      Console.WriteLine("No options    : {0}{1}", myT1, myStr.Substring([iS]))
      PrintMarker("           U" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "U" & ChrW(&H0308), [iS]), - 1)
      PrintMarker("           u" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "u" & ChrW(&H0308), [iS]), - 1)
      PrintMarker("            Ü : ", myComp.IndexOf(myStr, "Ü"c, [iS]), - 1)
      PrintMarker("            ü : ", myComp.IndexOf(myStr, "ü"c, [iS]), - 1)
      Console.WriteLine("Ordinal       : {0}{1}", myT1, myStr.Substring([iS]))
      PrintMarker("           U" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "U" & ChrW(&H0308), [iS], CompareOptions.Ordinal), - 1)
      PrintMarker("           u" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "u" & ChrW(&H0308), [iS], CompareOptions.Ordinal), - 1)
      PrintMarker("            Ü : ", myComp.IndexOf(myStr, "Ü"c, [iS], CompareOptions.Ordinal), - 1)
      PrintMarker("            ü : ", myComp.IndexOf(myStr, "ü"c, [iS], CompareOptions.Ordinal), - 1)
      Console.WriteLine("IgnoreCase    : {0}{1}", myT1, myStr.Substring([iS]))
      PrintMarker("           U" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "U" & ChrW(&H0308), [iS], CompareOptions.IgnoreCase), - 1)
      PrintMarker("           u" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "u" & ChrW(&H0308), [iS], CompareOptions.IgnoreCase), - 1)
      PrintMarker("            Ü : ", myComp.IndexOf(myStr, "Ü"c, [iS], CompareOptions.IgnoreCase), - 1)
      PrintMarker("            ü : ", myComp.IndexOf(myStr, "ü"c, [iS], CompareOptions.IgnoreCase), - 1)

      myT1 = New [String]("-"c, myStr.Length - [iS] - 1)
      Console.WriteLine("LastIndexOf( String, *, {0}, * )", [iS])
      Console.WriteLine("Original      : {0}", myStr)
      Console.WriteLine("No options    : {0}{1}", myStr.Substring(0, [iS] + 1), myT1)
      PrintMarker("           U" & ChrW(&H0308) & " : ", - 1, myComp.LastIndexOf(myStr, "U" & ChrW(&H0308), [iS]))
      PrintMarker("           u" & ChrW(&H0308) & " : ", - 1, myComp.LastIndexOf(myStr, "u" & ChrW(&H0308), [iS]))
      PrintMarker("            Ü : ", - 1, myComp.LastIndexOf(myStr, "Ü"c, [iS]))
      PrintMarker("            ü : ", - 1, myComp.LastIndexOf(myStr, "ü"c, [iS]))
      Console.WriteLine("Ordinal       : {0}{1}", myStr.Substring(0, [iS] + 1), myT1)
      PrintMarker("           U" & ChrW(&H0308) & " : ", - 1, myComp.LastIndexOf(myStr, "U" & ChrW(&H0308), [iS], CompareOptions.Ordinal))
      PrintMarker("           u" & ChrW(&H0308) & " : ", - 1, myComp.LastIndexOf(myStr, "u" & ChrW(&H0308), [iS], CompareOptions.Ordinal))
      PrintMarker("            Ü : ", - 1, myComp.LastIndexOf(myStr, "Ü"c, [iS], CompareOptions.Ordinal))
      PrintMarker("            ü : ", - 1, myComp.LastIndexOf(myStr, "ü"c, [iS], CompareOptions.Ordinal))
      Console.WriteLine("IgnoreCase    : {0}{1}", myStr.Substring(0, [iS] + 1), myT1)
      PrintMarker("           U" & ChrW(&H0308) & " : ", - 1, myComp.LastIndexOf(myStr, "U" & ChrW(&H0308), [iS], CompareOptions.IgnoreCase))
      PrintMarker("           u" & ChrW(&H0308) & " : ", - 1, myComp.LastIndexOf(myStr, "u" & ChrW(&H0308), [iS], CompareOptions.IgnoreCase))
      PrintMarker("            Ü : ", - 1, myComp.LastIndexOf(myStr, "Ü"c, [iS], CompareOptions.IgnoreCase))
      PrintMarker("            ü : ", - 1, myComp.LastIndexOf(myStr, "ü"c, [iS], CompareOptions.IgnoreCase))

   End Sub 'Main

   Public Shared Sub PrintMarker(Prefix As [String], First As Integer, Last As Integer)

      ' Determines the size of the array to create. 
      Dim mySize As Integer 
      If Last > First Then
         mySize = Last
      Else
         mySize = First
      End If  

      If mySize > - 1 Then 

         ' Creates an array of Char to hold the markers. 
         Dim myCharArr(mySize + 1) As [Char]

         ' Inserts the appropriate markers. 
         If First > - 1 Then
            myCharArr(First) = "f"c
         End If 
         If Last > - 1 Then
            myCharArr(Last) = "l"c
         End If 
         If First = Last Then
            myCharArr(First) = "b"c
         End If  

         ' Displays the array of Char as a String.
         Console.WriteLine("{0}{1}", Prefix, New [String](myCharArr))

      Else
         Console.WriteLine(Prefix)

      End If  

   End Sub 'PrintMarker

End Class 'SamplesCompareInfo 


'This code produces the following output. 

'IndexOf( String, *, 20, * ) 
'Original      : Is AE or ae the same as Æ or æ? 
'No options    : -------------------- as Æ or æ? 
'           AE :                         f 
'           ae :                              f 
'            Æ :                         f 
'            æ :                              f 
'Ordinal       : -------------------- as Æ or æ? 
'           AE : 
'           ae : 
'            Æ :                         f 
'            æ :                              f 
'IgnoreCase    : -------------------- as Æ or æ? 
'           AE :                         f 
'           ae :                         f 
'            Æ :                         f 
'            æ :                         f 
'LastIndexOf( String, *, 20, * ) 
'Original      : Is AE or ae the same as Æ or æ? 
'No options    : Is AE or ae the same ---------- 
'           AE :    l 
'           ae :          l 
'            Æ :    l 
'            æ :          l 
'Ordinal       : Is AE or ae the same ---------- 
'           AE :    l 
'           ae :          l 
'            Æ : 
'            æ : 
'IgnoreCase    : Is AE or ae the same ---------- 
'           AE :          l 
'           ae :          l 
'            Æ :          l 
'            æ :          l 

'IndexOf( String, *, 20, * ) 
'Original      : Is U" or u" the same as Ü or ü? 
'No options    : -------------------- as Ü or ü? 
'           U" :                         f 
'           u" :                              f 
'            Ü :                         f 
'            ü :                              f 
'Ordinal       : -------------------- as Ü or ü? 
'           U" : 
'           u" : 
'            Ü :                         f 
'            ü :                              f 
'IgnoreCase    : -------------------- as Ü or ü? 
'           U" :                         f 
'           u" :                         f 
'            Ü :                         f 
'            ü :                         f 
'LastIndexOf( String, *, 20, * ) 
'Original      : Is U" or u" the same as Ü or ü? 
'No options    : Is U" or u" the same ---------- 
'           U" :    l 
'           u" :          l 
'            Ü :    l 
'            ü :          l 
'Ordinal       : Is U" or u" the same ---------- 
'           U" :    l 
'           u" :          l 
'            Ü : 
'            ü : 
'IgnoreCase    : Is U" or u" the same ---------- 
'           U" :          l 
'           u" :          l 
'            Ü :          l 
'            ü :          l

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Portable Class Library

Supported in: Portable Class Library
Show:
© 2015 Microsoft