Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

CompareInfo.LastIndexOf Method (String, Char)

Searches for the specified character and returns the zero-based index of the last occurrence within the entire source string.

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

public virtual int LastIndexOf(
	string source,
	char value
)

Parameters

source
Type: System.String
The string to search.
value
Type: System.Char
The character to locate within source.

Return Value

Type: System.Int32
The zero-based index of the last occurrence of value within the entire source, if found; otherwise, -1.

ExceptionCondition
ArgumentNullException

source is null.

The source string is searched backward starting at the end of the string and ending at the beginning of the string.

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.

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


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;

      // Searches for the ligature Æ.
      String myStr = "Is AE or ae the same as Æ or æ?";
      outputBlock.Text += "\n";
      outputBlock.Text += String.Format("No options    : {0}", myStr) + "\n";
      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, 'Æ'), myComp.LastIndexOf(myStr, 'Æ'));
      PrintMarker(outputBlock, "            æ : ", myComp.IndexOf(myStr, 'æ'), myComp.LastIndexOf(myStr, 'æ'));
      outputBlock.Text += String.Format("Ordinal       : {0}", myStr) + "\n";
      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, 'Æ', CompareOptions.Ordinal), myComp.LastIndexOf(myStr, 'Æ', CompareOptions.Ordinal));
      PrintMarker(outputBlock, "            æ : ", myComp.IndexOf(myStr, 'æ', CompareOptions.Ordinal), myComp.LastIndexOf(myStr, 'æ', CompareOptions.Ordinal));
      outputBlock.Text += String.Format("IgnoreCase    : {0}", myStr) + "\n";
      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, 'Æ', CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, 'Æ', CompareOptions.IgnoreCase));
      PrintMarker(outputBlock, "            æ : ", myComp.IndexOf(myStr, 'æ', CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, 'æ', 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?";
      outputBlock.Text += "\n";
      outputBlock.Text += String.Format("No options    : {0}", myStr) + "\n";
      PrintMarker(outputBlock, "           U\u0308 : ", myComp.IndexOf(myStr, "U\u0308"), myComp.LastIndexOf(myStr, "U\u0308"));
      PrintMarker(outputBlock, "           u\u0308 : ", myComp.IndexOf(myStr, "u\u0308"), myComp.LastIndexOf(myStr, "u\u0308"));
      PrintMarker(outputBlock, "            Ü : ", myComp.IndexOf(myStr, 'Ü'), myComp.LastIndexOf(myStr, 'Ü'));
      PrintMarker(outputBlock, "            ü : ", myComp.IndexOf(myStr, 'ü'), myComp.LastIndexOf(myStr, 'ü'));
      outputBlock.Text += String.Format("Ordinal       : {0}", myStr) + "\n";
      PrintMarker(outputBlock, "           U\u0308 : ", myComp.IndexOf(myStr, "U\u0308", CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "U\u0308", CompareOptions.Ordinal));
      PrintMarker(outputBlock, "           u\u0308 : ", myComp.IndexOf(myStr, "u\u0308", CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "u\u0308", CompareOptions.Ordinal));
      PrintMarker(outputBlock, "            Ü : ", myComp.IndexOf(myStr, 'Ü', CompareOptions.Ordinal), myComp.LastIndexOf(myStr, 'Ü', CompareOptions.Ordinal));
      PrintMarker(outputBlock, "            ü : ", myComp.IndexOf(myStr, 'ü', CompareOptions.Ordinal), myComp.LastIndexOf(myStr, 'ü', CompareOptions.Ordinal));
      outputBlock.Text += String.Format("IgnoreCase    : {0}", myStr) + "\n";
      PrintMarker(outputBlock, "           U\u0308 : ", myComp.IndexOf(myStr, "U\u0308", CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "U\u0308", CompareOptions.IgnoreCase));
      PrintMarker(outputBlock, "           u\u0308 : ", myComp.IndexOf(myStr, "u\u0308", CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "u\u0308", CompareOptions.IgnoreCase));
      PrintMarker(outputBlock, "            Ü : ", myComp.IndexOf(myStr, 'Ü', CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, 'Ü', CompareOptions.IgnoreCase));
      PrintMarker(outputBlock, "            ü : ", myComp.IndexOf(myStr, 'ü', CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, 'ü', 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.

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

Show:
© 2014 Microsoft