System.String.LastIndexOf 方法 (String, Int32, Int32)

方法描述

报告指定字符串在此实例中的最后一个匹配项的索引位置。 搜索从指定字符位置开始,并检查指定数量的字符位置。

语法定义(C# System.String.LastIndexOf 方法 (String, Int32, Int32) 的用法)

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

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
value System-String 要搜寻的字符串。
startIndex System-Int32 搜索起始位置。
count System-Int32 要检查的字符位置数。
返回值 System.Int32 如果找到该字符串,则为 value 的从零开始的索引位置;如果未找到,则为 -1。或者如果当前实例等于 String.Empty 则也为 -1。 如果 value 为 Empty,则为 startIndex 和此实例中的最后一个索引位置中的较小者。

提示和注释

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

搜索从该实例的 startIndex 字符位置开始,沿反向向前搜索,直到找到 value 或已检查了 count 个字符位置。 例如,如果 startIndex 为 Length - 1,则该方法将从字符串中最后一个字符开始反向搜索 count 个字符。

此方法使用当前区域性执行单词(区分大小写和区域性)搜索。

对调用者的说明

如 在 .NET Framework 中使用字符串的最佳做法 中所述,我们建议您避免调用替换默认值的字符串比较方法,而是调用需要显式指定参数的方法。 若要使用当前区域性的比较规则执行此操作,请为它的 comparisonType 参数使用 StringComparison.CurrentCulture 的值来调用 LastIndexOf(String, Int32, Int32, StringComparison) 方法重载。

System.String.LastIndexOf 方法 (String, Int32, Int32)例子

下面的示例从某个子字符串的末尾到开头查找该子字符串中某个字符串的所有匹配项的索引。

// Sample for String.LastIndexOf(String, Int32, Int32)
using System;

class Sample {
    public static void Main() {

    string br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-";
    string br2 = "0123456789012345678901234567890123456789012345678901234567890123456";
    string str = "Now is the time for all good men to come to the aid of their party.";
    int start;
    int at;
    int count;
    int end;

    start = str.Length-1;
    end = start/2 - 1;
    Console.WriteLine("All occurrences of 'he' from position {0} to {1}.", start, end);
    Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str);
    Console.Write("The string 'he' occurs at position(s): ");

    count = 0;
    at = 0;
    while((start > -1) && (at > -1))
        {
        count = start - end; //Count must be within the substring.
        at = str.LastIndexOf("he", start, count);
        if (at > -1) 
            {
            Console.Write("{0} ", at);
            start = at - 1;
            }
        }
    Console.Write("{0}{0}{0}", Environment.NewLine);
    }
}
/*
This example produces the following results:
All occurrences of 'he' from position 66 to 32.
0----+----1----+----2----+----3----+----4----+----5----+----6----+-
0123456789012345678901234567890123456789012345678901234567890123456
Now is the time for all good men to come to the aid of their party.

The string 'he' occurs at position(s): 56 45
*/

异常

异常 异常描述
ArgumentNullException value 为 null。
ArgumentOutOfRangeException
  • count 为负。
  • 当前实例不等于 String.Empty,且 startIndex 是负数。
  • 当前实例不等于 String.Empty,且 startIndex 大于此实例的长度。
  • 当前实例不等于 String.Empty 和 startIndex - count + 1 指定一个不在此实例中的位置。

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。