Compartir a través de


CompareInfo.LastIndexOf Method (String, Char, Int32, Int32)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Searches for the specified character and returns the zero-based index of the last occurrence within the section of the source string that contains the specified number of elements and ends at the specified index.

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

Syntax

'Declaration
Public Overridable Function LastIndexOf ( _
    source As String, _
    value As Char, _
    startIndex As Integer, _
    count As Integer _
) As Integer
public virtual int LastIndexOf(
    string source,
    char value,
    int startIndex,
    int count
)

Parameters

  • value
    Type: System.Char
    The character to locate within source.
  • startIndex
    Type: System.Int32
    The zero-based starting index of the backward search.
  • count
    Type: System.Int32
    The number of elements in the section to search.

Return Value

Type: System.Int32
The zero-based index of the last occurrence of value within the section of source that contains the number of elements specified by count and ends at startIndex, if found; otherwise, -1.

Exceptions

Exception Condition
ArgumentNullException

source is nulla null reference (Nothing in Visual Basic).

ArgumentOutOfRangeException

startIndex is outside the range of valid indexes for source.

-or-

count is less than zero.

-or-

startIndex and count do not specify a valid section in source.

Remarks

The source string is searched backward starting at startIndex and ending at startIndex - count + 1.

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. To perform an ordinal (culture-insensitive) search, where a character is considered equivalent to another character only if the Unicode values are the same, the application should use one of the overloads that accepts a CompareOptions value as a parameter and use the Ordinal value.

Examples

The following example determines the indexes of the first and last occurrences of a character or a substring within a portion of 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

      ' iS is the starting index of the substring.
      Dim [iS] As Integer = 8
      ' iL is the length of the substring.
      Dim iL As Integer = 18
      ' myT1 and myT2 are the strings used for padding.
      Dim myT1 As New [String]("-"c, [iS])
      Dim myT2 As [String]

      ' Searches for the ligature Æ.
      Dim myStr As [String] = "Is AE or ae the same as Æ or æ?"
      myT2 = New [String]("-"c, myStr.Length - [iS] - iL)
      outputBlock.Text &= vbCrLf
      outputBlock.Text += String.Format("Original      : {0}", myStr) & vbCrLf
      outputBlock.Text += String.Format("No options    : {0}{1}{2}", myT1, myStr.Substring([iS], iL), myT2) & vbCrLf
      PrintMarker(outputBlock, "           AE : ", myComp.IndexOf(myStr, "AE", [iS], iL), myComp.LastIndexOf(myStr, "AE", [iS] + iL - 1, iL))
      PrintMarker(outputBlock, "           ae : ", myComp.IndexOf(myStr, "ae", [iS], iL), myComp.LastIndexOf(myStr, "ae", [iS] + iL - 1, iL))
      PrintMarker(outputBlock, "            Æ : ", myComp.IndexOf(myStr, "Æ"c, [iS], iL), myComp.LastIndexOf(myStr, "Æ"c, [iS] + iL - 1, iL))
      PrintMarker(outputBlock, "            æ : ", myComp.IndexOf(myStr, "æ"c, [iS], iL), myComp.LastIndexOf(myStr, "æ"c, [iS] + iL - 1, iL))
      outputBlock.Text += String.Format("Ordinal       : {0}{1}{2}", myT1, myStr.Substring([iS], iL), myT2) & vbCrLf
      PrintMarker(outputBlock, "           AE : ", myComp.IndexOf(myStr, "AE", [iS], iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "AE", [iS] + iL - 1, iL, CompareOptions.Ordinal))
      PrintMarker(outputBlock, "           ae : ", myComp.IndexOf(myStr, "ae", [iS], iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "ae", [iS] + iL - 1, iL, CompareOptions.Ordinal))
      PrintMarker(outputBlock, "            Æ : ", myComp.IndexOf(myStr, "Æ"c, [iS], iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "Æ"c, [iS] + iL - 1, iL, CompareOptions.Ordinal))
      PrintMarker(outputBlock, "            æ : ", myComp.IndexOf(myStr, "æ"c, [iS], iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "æ"c, [iS] + iL - 1, iL, CompareOptions.Ordinal))
      outputBlock.Text += String.Format("IgnoreCase    : {0}{1}{2}", myT1, myStr.Substring([iS], iL), myT2) & vbCrLf
      PrintMarker(outputBlock, "           AE : ", myComp.IndexOf(myStr, "AE", [iS], iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "AE", [iS] + iL - 1, iL, CompareOptions.IgnoreCase))
      PrintMarker(outputBlock, "           ae : ", myComp.IndexOf(myStr, "ae", [iS], iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "ae", [iS] + iL - 1, iL, CompareOptions.IgnoreCase))
      PrintMarker(outputBlock, "            Æ : ", myComp.IndexOf(myStr, "Æ"c, [iS], iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "Æ"c, [iS] + iL - 1, iL, CompareOptions.IgnoreCase))
      PrintMarker(outputBlock, "            æ : ", myComp.IndexOf(myStr, "æ"c, [iS], iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "æ"c, [iS] + iL - 1, iL, 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) & "?"
      myT2 = New [String]("-"c, myStr.Length - [iS] - iL)
      outputBlock.Text &= vbCrLf
      outputBlock.Text += String.Format("Original      : {0}", myStr) & vbCrLf
      outputBlock.Text += String.Format("No options    : {0}{1}{2}", myT1, myStr.Substring([iS], iL), myT2) & vbCrLf
      PrintMarker(outputBlock, "           U" & ChrW(&H308) & " : ", myComp.IndexOf(myStr, "U" & ChrW(&H308), [iS], iL), myComp.LastIndexOf(myStr, "U" & ChrW(&H308), [iS] + iL - 1, iL))
      PrintMarker(outputBlock, "           u" & ChrW(&H308) & " : ", myComp.IndexOf(myStr, "u" & ChrW(&H308), [iS], iL), myComp.LastIndexOf(myStr, "u" & ChrW(&H308), [iS] + iL - 1, iL))
      PrintMarker(outputBlock, "            Ü : ", myComp.IndexOf(myStr, "Ü"c, [iS], iL), myComp.LastIndexOf(myStr, "Ü"c, [iS] + iL - 1, iL))
      PrintMarker(outputBlock, "            ü : ", myComp.IndexOf(myStr, "ü"c, [iS], iL), myComp.LastIndexOf(myStr, "ü"c, [iS] + iL - 1, iL))
      outputBlock.Text += String.Format("Ordinal       : {0}{1}{2}", myT1, myStr.Substring([iS], iL), myT2) & vbCrLf
      PrintMarker(outputBlock, "           U" & ChrW(&H308) & " : ", myComp.IndexOf(myStr, "U" & ChrW(&H308), [iS], iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "U" & ChrW(&H308), [iS] + iL - 1, iL, CompareOptions.Ordinal))
      PrintMarker(outputBlock, "           u" & ChrW(&H308) & " : ", myComp.IndexOf(myStr, "u" & ChrW(&H308), [iS], iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "u" & ChrW(&H308), [iS] + iL - 1, iL, CompareOptions.Ordinal))
      PrintMarker(outputBlock, "            Ü : ", myComp.IndexOf(myStr, "Ü"c, [iS], iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "Ü"c, [iS] + iL - 1, iL, CompareOptions.Ordinal))
      PrintMarker(outputBlock, "            ü : ", myComp.IndexOf(myStr, "ü"c, [iS], iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "ü"c, [iS] + iL - 1, iL, CompareOptions.Ordinal))
      outputBlock.Text += String.Format("IgnoreCase    : {0}{1}{2}", myT1, myStr.Substring([iS], iL), myT2) & vbCrLf
      PrintMarker(outputBlock, "           U" & ChrW(&H308) & " : ", myComp.IndexOf(myStr, "U" & ChrW(&H308), [iS], iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "U" & ChrW(&H308), [iS] + iL - 1, iL, CompareOptions.IgnoreCase))
      PrintMarker(outputBlock, "           u" & ChrW(&H308) & " : ", myComp.IndexOf(myStr, "u" & ChrW(&H308), [iS], iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "u" & ChrW(&H308), [iS] + iL - 1, iL, CompareOptions.IgnoreCase))
      PrintMarker(outputBlock, "            Ü : ", myComp.IndexOf(myStr, "Ü"c, [iS], iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "Ü"c, [iS] + iL - 1, iL, CompareOptions.IgnoreCase))
      PrintMarker(outputBlock, "            ü : ", myComp.IndexOf(myStr, "ü"c, [iS], iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "ü"c, [iS] + iL - 1, iL, 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.
'
'Original      : Is AE or ae the same as Æ or æ?
'No options    : -------- ae the same as Æ -----
'           AE :                         b
'           ae :          b
'            Æ :                         b
'            æ :          b
'Ordinal       : -------- ae the same as Æ -----
'           AE :
'           ae :          b
'            Æ :                         b
'            æ :
'IgnoreCase    : -------- ae the same as Æ -----
'           AE :          f              l
'           ae :          f              l
'            Æ :          f              l
'            æ :          f              l
'
'Original      : Is U" or u" the same as Ü or ü?
'No options    : -------- u" the same as Ü -----
'           U" :                         b
'           u" :          b
'            Ü :                         b
'            ü :          b
'Ordinal       : -------- u" the same as Ü -----
'           U" :
'           u" :          b
'            Ü :                         b
'            ü :
'IgnoreCase    : -------- u" the same as Ü -----
'           U" :          f              l
'           u" :          f              l
'            Ü :          f              l
'            ü :          f              l

using System;
using System.Globalization;

public class Example
{

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {

      // Creates CompareInfo for the InvariantCulture.
      CompareInfo myComp = CultureInfo.InvariantCulture.CompareInfo;

      // iS is the starting index of the substring.
      int iS = 8;
      // iL is the length of the substring.
      int iL = 18;
      // myT1 and myT2 are the strings used for padding.
      String myT1 = new String('-', iS);
      String myT2;

      // Searches for the ligature Æ.
      String myStr = "Is AE or ae the same as Æ or æ?";
      myT2 = new String('-', myStr.Length - iS - iL);
      outputBlock.Text += "\n";
      outputBlock.Text += String.Format("Original      : {0}", myStr) + "\n";
      outputBlock.Text += String.Format("No options    : {0}{1}{2}", myT1, myStr.Substring(iS, iL), myT2) + "\n";
      PrintMarker(outputBlock, "           AE : ", myComp.IndexOf(myStr, "AE", iS, iL), myComp.LastIndexOf(myStr, "AE", iS + iL - 1, iL));
      PrintMarker(outputBlock, "           ae : ", myComp.IndexOf(myStr, "ae", iS, iL), myComp.LastIndexOf(myStr, "ae", iS + iL - 1, iL));
      PrintMarker(outputBlock, "            Æ : ", myComp.IndexOf(myStr, 'Æ', iS, iL), myComp.LastIndexOf(myStr, 'Æ', iS + iL - 1, iL));
      PrintMarker(outputBlock, "            æ : ", myComp.IndexOf(myStr, 'æ', iS, iL), myComp.LastIndexOf(myStr, 'æ', iS + iL - 1, iL));
      outputBlock.Text += String.Format("Ordinal       : {0}{1}{2}", myT1, myStr.Substring(iS, iL), myT2) + "\n";
      PrintMarker(outputBlock, "           AE : ", myComp.IndexOf(myStr, "AE", iS, iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "AE", iS + iL - 1, iL, CompareOptions.Ordinal));
      PrintMarker(outputBlock, "           ae : ", myComp.IndexOf(myStr, "ae", iS, iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "ae", iS + iL - 1, iL, CompareOptions.Ordinal));
      PrintMarker(outputBlock, "            Æ : ", myComp.IndexOf(myStr, 'Æ', iS, iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, 'Æ', iS + iL - 1, iL, CompareOptions.Ordinal));
      PrintMarker(outputBlock, "            æ : ", myComp.IndexOf(myStr, 'æ', iS, iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, 'æ', iS + iL - 1, iL, CompareOptions.Ordinal));
      outputBlock.Text += String.Format("IgnoreCase    : {0}{1}{2}", myT1, myStr.Substring(iS, iL), myT2) + "\n";
      PrintMarker(outputBlock, "           AE : ", myComp.IndexOf(myStr, "AE", iS, iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "AE", iS + iL - 1, iL, CompareOptions.IgnoreCase));
      PrintMarker(outputBlock, "           ae : ", myComp.IndexOf(myStr, "ae", iS, iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "ae", iS + iL - 1, iL, CompareOptions.IgnoreCase));
      PrintMarker(outputBlock, "            Æ : ", myComp.IndexOf(myStr, 'Æ', iS, iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, 'Æ', iS + iL - 1, iL, CompareOptions.IgnoreCase));
      PrintMarker(outputBlock, "            æ : ", myComp.IndexOf(myStr, 'æ', iS, iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, 'æ', iS + iL - 1, iL, CompareOptions.IgnoreCase));

      // Searches for the combining character sequence Latin capital letter U with diaeresis or Latin small letter u with diaeresis.
      myStr = "Is \u0055\u0308 or \u0075\u0308 the same as \u00DC or \u00FC?";
      myT2 = new String('-', myStr.Length - iS - iL);
      outputBlock.Text += "\n";
      outputBlock.Text += String.Format("Original      : {0}", myStr) + "\n";
      outputBlock.Text += String.Format("No options    : {0}{1}{2}", myT1, myStr.Substring(iS, iL), myT2) + "\n";
      PrintMarker(outputBlock, "           U\u0308 : ", myComp.IndexOf(myStr, "U\u0308", iS, iL), myComp.LastIndexOf(myStr, "U\u0308", iS + iL - 1, iL));
      PrintMarker(outputBlock, "           u\u0308 : ", myComp.IndexOf(myStr, "u\u0308", iS, iL), myComp.LastIndexOf(myStr, "u\u0308", iS + iL - 1, iL));
      PrintMarker(outputBlock, "            Ü : ", myComp.IndexOf(myStr, 'Ü', iS, iL), myComp.LastIndexOf(myStr, 'Ü', iS + iL - 1, iL));
      PrintMarker(outputBlock, "            ü : ", myComp.IndexOf(myStr, 'ü', iS, iL), myComp.LastIndexOf(myStr, 'ü', iS + iL - 1, iL));
      outputBlock.Text += String.Format("Ordinal       : {0}{1}{2}", myT1, myStr.Substring(iS, iL), myT2) + "\n";
      PrintMarker(outputBlock, "           U\u0308 : ", myComp.IndexOf(myStr, "U\u0308", iS, iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "U\u0308", iS + iL - 1, iL, CompareOptions.Ordinal));
      PrintMarker(outputBlock, "           u\u0308 : ", myComp.IndexOf(myStr, "u\u0308", iS, iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "u\u0308", iS + iL - 1, iL, CompareOptions.Ordinal));
      PrintMarker(outputBlock, "            Ü : ", myComp.IndexOf(myStr, 'Ü', iS, iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, 'Ü', iS + iL - 1, iL, CompareOptions.Ordinal));
      PrintMarker(outputBlock, "            ü : ", myComp.IndexOf(myStr, 'ü', iS, iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, 'ü', iS + iL - 1, iL, CompareOptions.Ordinal));
      outputBlock.Text += String.Format("IgnoreCase    : {0}{1}{2}", myT1, myStr.Substring(iS, iL), myT2) + "\n";
      PrintMarker(outputBlock, "           U\u0308 : ", myComp.IndexOf(myStr, "U\u0308", iS, iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "U\u0308", iS + iL - 1, iL, CompareOptions.IgnoreCase));
      PrintMarker(outputBlock, "           u\u0308 : ", myComp.IndexOf(myStr, "u\u0308", iS, iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "u\u0308", iS + iL - 1, iL, CompareOptions.IgnoreCase));
      PrintMarker(outputBlock, "            Ü : ", myComp.IndexOf(myStr, 'Ü', iS, iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, 'Ü', iS + iL - 1, iL, CompareOptions.IgnoreCase));
      PrintMarker(outputBlock, "            ü : ", myComp.IndexOf(myStr, 'ü', iS, iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, 'ü', iS + iL - 1, iL, CompareOptions.IgnoreCase));

   }

   public static void PrintMarker(System.Windows.Controls.TextBlock outputBlock, String Prefix, int First, int Last)
   {

      // Determines the size of the array to create.
      int mySize;
      if (Last > First)
         mySize = Last;
      else
         mySize = First;

      if (mySize > -1)
      {

         // Creates an array of Char to hold the markers.
         Char[] myCharArr = new Char[mySize + 1];

         // Inserts the appropriate markers.
         if (First > -1)
            myCharArr[First] = 'f';
         if (Last > -1)
            myCharArr[Last] = 'l';
         if (First == Last)
            myCharArr[First] = 'b';

         // Displays the array of Char as a String.
         outputBlock.Text += String.Format("{0}{1}", Prefix, new String(myCharArr)) + "\n";

      }
      else
         outputBlock.Text += Prefix + "\n";

   }

}


/*
This code produces the following output.

Original      : Is AE or ae the same as Æ or æ?
No options    : -------- ae the same as Æ -----
           AE :                         b
           ae :          b
            Æ :                         b
            æ :          b
Ordinal       : -------- ae the same as Æ -----
           AE :
           ae :          b
            Æ :                         b
            æ :
IgnoreCase    : -------- ae the same as Æ -----
           AE :          f              l
           ae :          f              l
            Æ :          f              l
            æ :          f              l

Original      : Is U" or u" the same as Ü or ü?
No options    : -------- u" the same as Ü -----
           U" :                         b
           u" :          b
            Ü :                         b
            ü :          b
Ordinal       : -------- u" the same as Ü -----
           U" :
           u" :          b
            Ü :                         b
            ü :
IgnoreCase    : -------- u" the same as Ü -----
           U" :          f              l
           u" :          f              l
            Ü :          f              l
            ü :          f              l

*/

Version Information

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

Platforms

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