LastIndexOf 方法 (String, StringComparison)
此文章由机器翻译。 将光标移到文章的句子上,以查看原文。 更多信息。
译文
原文

String.LastIndexOf 方法 (String, StringComparison)

 

报告指定字符串在当前 String 对象中最后一个匹配项的从零开始的索引。一个参数指定要用于指定字符串的搜索类型。

命名空间:   System
程序集:  mscorlib(mscorlib.dll 中)

public int LastIndexOf(
	string value,
	StringComparison comparisonType
)

参数

value

要搜寻的字符串。

comparisonType

指定搜索规则的枚举值之一。

返回值

Type: System.Int32

如果找到该字符串,则为 value 参数的从零开始的起始索引位置;如果未找到该字符串,则为 -1。如果 valueString.Empty,则返回值为此实例中的最后一个索引位置。

Exception Condition
ArgumentNullException

valuenull

ArgumentException

comparisonType 不是有效 System.StringComparison 值。

编号从零开始的索引。也就是说,在字符串中的第一个字符位于索引零开始,最后一个位于 Length -1。

comparisonType 参数指定要搜索 value 参数使用当前或固定区域性、 使用区分大小写或不区分大小写的搜索,以及使用 word 或序号比较规则。

搜索此实例的最后一个字符位置开始,沿反向向前直到开始 value 找到或检查完第一个字符位置为止。

对调用方的说明:

字符集包括可忽略字符,在执行语言性的或区分区域性的比较时该字符不被考虑。在区分区域性的搜索 (即,如果 options 不是 StringComparison.OrdinalStringComparison.OrdinalIgnoreCase) 中,如果 value 包含一个可忽略字符,则结果与移除了该字符的搜索等效。如果 value 仅包含一个或多个可忽略字符, LastIndexOf 方法始终返回 String.Length – 1,表示当前实例中的最后一个索引位置。

在下面的示例中, LastIndexOf(String, StringComparison) 方法用于在两个字符串中找到三个的子字符串 (软连字符 (U + 00AD)、 软连字符后跟"n"和软连字符后跟"m")。只有一个字符串包含软连字符。如果在上运行该示例 .NET Framework 4 或更高版本,因为软连字符是一个可忽略字符,则区分区域性的搜索返回的相同值,它将返回该软连字符如果未包含在搜索字符串。顺序搜索,但是,成功在一个字符串中查找该软连字符,并报告不存在从第二个字符串。

using System;

public class Example
{
   public static void Main()
   {
      string s1 = "ani\u00ADmal";
      string s2 = "animal";

      Console.WriteLine("Culture-sensitive comparison:");
      // Use culture-sensitive comparison to find the last soft hyphen.
      Console.WriteLine(s1.LastIndexOf("\u00AD", StringComparison.CurrentCulture));
      Console.WriteLine(s2.LastIndexOf("\u00AD", StringComparison.CurrentCulture));

      // Use culture-sensitive comparison to find the last soft hyphen followed by "n".
      Console.WriteLine(s1.LastIndexOf("\u00ADn", StringComparison.CurrentCulture));
      Console.WriteLine(s2.LastIndexOf("\u00ADn", StringComparison.CurrentCulture));

      // Use culture-sensitive comparison to find the last soft hyphen followed by "m".
      Console.WriteLine(s1.LastIndexOf("\u00ADm", StringComparison.CurrentCulture));
      Console.WriteLine(s2.LastIndexOf("\u00ADm", StringComparison.CurrentCulture));

      Console.WriteLine("Ordinal comparison:");
      // Use ordinal comparison to find the last soft hyphen.
      Console.WriteLine(s1.LastIndexOf("\u00AD", StringComparison.Ordinal));
      Console.WriteLine(s2.LastIndexOf("\u00AD", StringComparison.Ordinal));

      // Use ordinal comparison to find the last soft hyphen followed by "n".
      Console.WriteLine(s1.LastIndexOf("\u00ADn", StringComparison.Ordinal));
      Console.WriteLine(s2.LastIndexOf("\u00ADn", StringComparison.Ordinal));

      // Use ordinal comparison to find the last soft hyphen followed by "m".
      Console.WriteLine(s1.LastIndexOf("\u00ADm", StringComparison.Ordinal));
      Console.WriteLine(s2.LastIndexOf("\u00ADm", StringComparison.Ordinal));
   }
}
// The example displays the following output:
//       6
//       5
//       1
//       1
//       4
//       3
//       Ordinal comparison:
//       3
//       -1
//       -1
//       -1
//       3
//       -1

下面的示例演示的三个重载 LastIndexOf 查找内使用的不同值的另一个字符串的字符串的最后一个匹配项的方法 StringComparison 枚举。

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

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

class Sample 
{
    public static void Main() 
    {
    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 };

// Clear the screen and display an introduction.
    Console.Clear();
    Console.WriteLine(intro);

// 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");
    Console.WriteLine("The current culture is \"{0}\" - {1}.", 
                       Thread.CurrentThread.CurrentCulture.Name,
                       Thread.CurrentThread.CurrentCulture.DisplayName);

// Display the string to search for and the string to search.
    Console.WriteLine("Search for the string \"{0}\" in the string \"{1}\"", 
                       CapitalAWithRing, cat);
    Console.WriteLine();

// 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. 

    Console.WriteLine("Part 1: Start index and count are specified.");
    foreach (StringComparison sc in scValues)
        {
        loc = cat.LastIndexOf(CapitalAWithRing, cat.Length-1, cat.Length, sc);
        Console.WriteLine(resultFmt, sc, loc);
        }

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

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

/*
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

*/

Universal Windows Platform
4.5 后可用
.NET Framework
2.0 后可用
Portable Class Library
受以下版本支持:portable .NET platforms
Silverlight
2.0 后可用
Windows Phone Silverlight
7.0 后可用
Windows Phone
8.1 后可用
返回页首
显示:
© 2016 Microsoft