Export (0) Print
Expand All

CompareInfo.LastIndexOf Method (String, String, CompareOptions)

Searches for the specified substring and returns the zero-based index of the last occurrence within the entire source string using the specified CompareOptions value.

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

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

Parameters

source
Type: System.String
The string to search.
value
Type: System.String
The string to locate within source.
options
Type: System.Globalization.CompareOptions
The CompareOptions value that defines how source and value should be compared. options is either the value Ordinal used by itself, or the 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 last occurrence of value within the entire source using the specified CompareOptions value, if found; otherwise, -1.

ExceptionCondition
ArgumentNullException

source is Nothing.

-or-

value is Nothing.

ArgumentException

options contains an invalid CompareOptions value.

The source string is searched backward starting at the end of the string and ending at the beginning 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. A Unicode 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. If options includes the Ordinal value, this overload performs an ordinal (culture-insensitive) search, where the Unicode values are compared.

The following example determines the indexes of the first and last occurrences of a character or a substring within a string.


Imports System.Globalization

Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

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

      ' Searches for the ligature Æ.
      Dim myStr As [String] = "Is AE or ae the same as Æ or æ?"
      outputBlock.Text &= vbCrLf
      outputBlock.Text += String.Format("No options    : {0}", myStr) & vbCrLf
      PrintMarker(outputBlock, "           AE : ", myComp.IndexOf(myStr, "AE"), myComp.LastIndexOf(myStr, "AE"))
      PrintMarker(outputBlock, "           ae : ", myComp.IndexOf(myStr, "ae"), myComp.LastIndexOf(myStr, "ae"))
      PrintMarker(outputBlock, "            Æ : ", myComp.IndexOf(myStr, "Æ"c), myComp.LastIndexOf(myStr, "Æ"c))
      PrintMarker(outputBlock, "            æ : ", myComp.IndexOf(myStr, "æ"c), myComp.LastIndexOf(myStr, "æ"c))
      outputBlock.Text += String.Format("Ordinal       : {0}", myStr) & vbCrLf
      PrintMarker(outputBlock, "           AE : ", myComp.IndexOf(myStr, "AE", CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "AE", CompareOptions.Ordinal))
      PrintMarker(outputBlock, "           ae : ", myComp.IndexOf(myStr, "ae", CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "ae", CompareOptions.Ordinal))
      PrintMarker(outputBlock, "            Æ : ", myComp.IndexOf(myStr, "Æ"c, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "Æ"c, CompareOptions.Ordinal))
      PrintMarker(outputBlock, "            æ : ", myComp.IndexOf(myStr, "æ"c, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "æ"c, CompareOptions.Ordinal))
      outputBlock.Text += String.Format("IgnoreCase    : {0}", myStr) & vbCrLf
      PrintMarker(outputBlock, "           AE : ", myComp.IndexOf(myStr, "AE", CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "AE", CompareOptions.IgnoreCase))
      PrintMarker(outputBlock, "           ae : ", myComp.IndexOf(myStr, "ae", CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "ae", CompareOptions.IgnoreCase))
      PrintMarker(outputBlock, "            Æ : ", myComp.IndexOf(myStr, "Æ"c, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "Æ"c, CompareOptions.IgnoreCase))
      PrintMarker(outputBlock, "            æ : ", myComp.IndexOf(myStr, "æ"c, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "æ"c, CompareOptions.IgnoreCase))

      ' Searches for the combining character sequence Latin capital letter U with diaeresis or Latin small letter u with diaeresis.
      myStr = "Is " & ChrW(&H55) & ChrW(&H308) & " or " & ChrW(&H75) & ChrW(&H308) & " the same as " & ChrW(&HDC) & " or " & ChrW(&HFC) & "?"
      outputBlock.Text &= vbCrLf
      outputBlock.Text += String.Format("No options    : {0}", myStr) & vbCrLf
      PrintMarker(outputBlock, "           U" & ChrW(&H308) & " : ", myComp.IndexOf(myStr, "U" & ChrW(&H308)), myComp.LastIndexOf(myStr, "U" & ChrW(&H308)))
      PrintMarker(outputBlock, "           u" & ChrW(&H308) & " : ", myComp.IndexOf(myStr, "u" & ChrW(&H308)), myComp.LastIndexOf(myStr, "u" & ChrW(&H308)))
      PrintMarker(outputBlock, "            Ü : ", myComp.IndexOf(myStr, "Ü"c), myComp.LastIndexOf(myStr, "Ü"c))
      PrintMarker(outputBlock, "            ü : ", myComp.IndexOf(myStr, "ü"c), myComp.LastIndexOf(myStr, "ü"c))
      outputBlock.Text += String.Format("Ordinal       : {0}", myStr) & vbCrLf
      PrintMarker(outputBlock, "           U" & ChrW(&H308) & " : ", myComp.IndexOf(myStr, "U" & ChrW(&H308), CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "U" & ChrW(&H308), CompareOptions.Ordinal))
      PrintMarker(outputBlock, "           u" & ChrW(&H308) & " : ", myComp.IndexOf(myStr, "u" & ChrW(&H308), CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "u" & ChrW(&H308), CompareOptions.Ordinal))
      PrintMarker(outputBlock, "            Ü : ", myComp.IndexOf(myStr, "Ü"c, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "Ü"c, CompareOptions.Ordinal))
      PrintMarker(outputBlock, "            ü : ", myComp.IndexOf(myStr, "ü"c, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "ü"c, CompareOptions.Ordinal))
      outputBlock.Text += String.Format("IgnoreCase    : {0}", myStr) & vbCrLf
      PrintMarker(outputBlock, "           U" & ChrW(&H308) & " : ", myComp.IndexOf(myStr, "U" & ChrW(&H308), CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "U" & ChrW(&H308), CompareOptions.IgnoreCase))
      PrintMarker(outputBlock, "           u" & ChrW(&H308) & " : ", myComp.IndexOf(myStr, "u" & ChrW(&H308), CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "u" & ChrW(&H308), CompareOptions.IgnoreCase))
      PrintMarker(outputBlock, "            Ü : ", myComp.IndexOf(myStr, "Ü"c, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "Ü"c, CompareOptions.IgnoreCase))
      PrintMarker(outputBlock, "            ü : ", myComp.IndexOf(myStr, "ü"c, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "ü"c, CompareOptions.IgnoreCase))

   End Sub 'Main

   Public Shared Sub PrintMarker(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal Prefix As [String], ByVal First As Integer, ByVal 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.
         outputBlock.Text += String.Format("{0}{1}", Prefix, New [String](myCharArr)) & vbCrLf

      Else
         outputBlock.Text &= Prefix & vbCrLf

      End If

   End Sub 'PrintMarker

End Class 'SamplesCompareInfo 


'This code produces the following output.
'
'No options    : Is AE or ae the same as Æ or æ?
'           AE :    f                    l
'           ae :          f                   l
'            Æ :    f                    l
'            æ :          f                   l
'Ordinal       : Is AE or ae the same as Æ or æ?
'           AE :    b
'           ae :          b
'            Æ :                         b
'            æ :                              b
'IgnoreCase    : Is AE or ae the same as Æ or æ?
'           AE :    f                         l
'           ae :    f                         l
'            Æ :    f                         l
'            æ :    f                         l
'
'No options    : Is U" or u" the same as Ü or ü?
'           U" :    f                    l
'           u" :          f                   l
'            Ü :    f                    l
'            ü :          f                   l
'Ordinal       : Is U" or u" the same as Ü or ü?
'           U" :    b
'           u" :          b
'            Ü :                         b
'            ü :                              b
'IgnoreCase    : Is U" or u" the same as Ü or ü?
'           U" :    f                         l
'           u" :    f                         l
'            Ü :    f                         l
'            ü :    f                         l



Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show:
© 2014 Microsoft