String.LastIndexOf Method (String, Int32, Int32, StringComparison)

July 28, 2014

Reports the zero-based index position of the last occurrence of a specified string within this instance. The search starts at a specified character position and proceeds backward toward the beginning of the string for the specified number of character positions. A parameter specifies the type of comparison to perform when searching for the specified string.

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

public int LastIndexOf(
	string value,
	int startIndex,
	int count,
	StringComparison comparisonType
)

Parameters

value
Type: System.String
The string to seek.
startIndex
Type: System.Int32
The search starting position. The search proceeds from startIndex toward the beginning of this instance.
count
Type: System.Int32
The number of character positions to examine.
comparisonType
Type: System.StringComparison
One of the enumeration values that specifies the rules for the search.

Return Value

Type: System.Int32
The zero-based index position of the value parameter if that string is found, or -1 if it is not found or if the current instance equals String.Empty. If value is String.Empty, the return value is the smaller of startIndex and the last index position in this instance.

ExceptionCondition
ArgumentNullException

value is null.

ArgumentOutOfRangeException

count is negative.

-or-

The current instance does not equal String.Empty, and startIndex is negative.

-or-

The current instance does not equal String.Empty, and startIndex is greater than the length of this instance.

-or-

The current instance does not equal String.Empty, and startIndex + 1 - count specifies a position that is not within this instance.

ArgumentException

comparisonType is not a valid System.StringComparison value.

Index numbering starts from zero. That is, the first character in the string is at index zero and the last is at Length - 1.

The search begins at the startIndex character position and proceeds backward until either value is found or count character positions have been examined. For example, if startIndex is Length - 1, the method searches backward count characters from the last character in the string.

The comparisonType parameter specifies to search for the value parameter using the current or invariant culture, using a case-sensitive or case-insensitive search, and using word or ordinal comparison rules.

The following code example demonstrates three overloads of the LastIndexOf method that find the last occurrence of a string within another string using different values of the StringComparison enumeration.


// This code example demonstrates the 
// System.String.LastIndexOf(String, ..., StringComparison) methods.

using System;
using System.Threading;
using System.Globalization;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      string intro = "Find the last occurrence of a character using different " +
                     "values of StringComparison.";
      string resultFmt = "Comparison: {0,-28} Location: {1,3}";

      // Define a string to search for.
      // U+00c5 = LATIN CAPITAL LETTER A WITH RING ABOVE
      string CapitalAWithRing = "\u00c5";

      // Define a string to search. 
      // The result of combining the characters LATIN SMALL LETTER A and COMBINING 
      // RING ABOVE (U+0061, U+030a) is linguistically equivalent to the character 
      // LATIN SMALL LETTER A WITH RING ABOVE (U+00e5).
      string cat = "A Cheshire c" + "\u0061\u030a" + "t";
      int loc = 0;
      StringComparison[] scValues = {
        StringComparison.CurrentCulture,
        StringComparison.CurrentCultureIgnoreCase,
        StringComparison.InvariantCulture,
        StringComparison.InvariantCultureIgnoreCase,
        StringComparison.Ordinal,
        StringComparison.OrdinalIgnoreCase };

      // Display an introduction.
      outputBlock.Text += intro + "\n";

      // Display the current culture because culture affects the result. For example, 
      // try this code example with the "sv-SE" (Swedish-Sweden) culture.

      Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
      outputBlock.Text += String.Format("The current culture is \"{0}\" - {1}.",
                         Thread.CurrentThread.CurrentCulture.Name,
                         Thread.CurrentThread.CurrentCulture.DisplayName) + "\n";

      // Display the string to search for and the string to search.
      outputBlock.Text += String.Format("Search for the string \"{0}\" in the string \"{1}\"",
                         CapitalAWithRing, cat) + "\n";
      outputBlock.Text += "\n";

      // Note that in each of the following searches, we look for 
      // LATIN CAPITAL LETTER A WITH RING ABOVE in a string that contains 
      // LATIN SMALL LETTER A WITH RING ABOVE. A result value of -1 indicates 
      // the string was not found.
      // Search using different values of StringComparsion. Specify the start 
      // index and count. 

      outputBlock.Text += "Part 1: Start index and count are specified." + "\n";
      foreach (StringComparison sc in scValues)
      {
         loc = cat.LastIndexOf(CapitalAWithRing, cat.Length - 1, cat.Length, sc);
         outputBlock.Text += String.Format(resultFmt, sc, loc) + "\n";
      }

      // Search using different values of StringComparsion. Specify the 
      // start index. 
      outputBlock.Text += "\nPart 2: Start index is specified." + "\n";
      foreach (StringComparison sc in scValues)
      {
         loc = cat.LastIndexOf(CapitalAWithRing, cat.Length - 1, sc);
         outputBlock.Text += String.Format(resultFmt, sc, loc) + "\n";
      }

      // Search using different values of StringComparsion. 
      outputBlock.Text += "\nPart 3: Neither start index nor count is specified." + "\n";
      foreach (StringComparison sc in scValues)
      {
         loc = cat.LastIndexOf(CapitalAWithRing, sc);
         outputBlock.Text += String.Format(resultFmt, sc, loc) + "\n";
      }
   }
}

/*
Note: This code example was executed on a console whose user interface 
culture is "en-US" (English-United States).

This code example produces the following results:

Find the last occurrence of a character using different values of StringComparison.
The current culture is "en-US" - English (United States).
Search for the string "Å" in the string "A Cheshire ca°t"

Part 1: Start index and count are specified.
Comparison: CurrentCulture               Location:  -1
Comparison: CurrentCultureIgnoreCase     Location:  12
Comparison: InvariantCulture             Location:  -1
Comparison: InvariantCultureIgnoreCase   Location:  12
Comparison: Ordinal                      Location:  -1
Comparison: OrdinalIgnoreCase            Location:  -1

Part 2: Start index is specified.
Comparison: CurrentCulture               Location:  -1
Comparison: CurrentCultureIgnoreCase     Location:  12
Comparison: InvariantCulture             Location:  -1
Comparison: InvariantCultureIgnoreCase   Location:  12
Comparison: Ordinal                      Location:  -1
Comparison: OrdinalIgnoreCase            Location:  -1

Part 3: Neither start index nor count is specified.
Comparison: CurrentCulture               Location:  -1
Comparison: CurrentCultureIgnoreCase     Location:  12
Comparison: InvariantCulture             Location:  -1
Comparison: InvariantCultureIgnoreCase   Location:  12
Comparison: Ordinal                      Location:  -1
Comparison: OrdinalIgnoreCase            Location:  -1

*/


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft