C# String LastIndexOf() 字符串最后索引查找方法
LastIndexOf()
方法返回指定字符或字符串在给定字符串内的最后一次出现的索引位置。
示例
using System;
namespace CsharpString {
class Test {
public static void Main(string [] args) {
string str = "Icecream";
// 返回 'c' 的最后一次出现的索引位置
int index = str.LastIndexOf('c');
Console.WriteLine(index);
Console.ReadLine();
}
}
}
// 输出: 3
LastIndexOf() 语法
字符串 LastIndexOf()
方法的语法如下:
LastIndexOf(String value, int startIndex, int count, StringComparison comparisonType)
在这里,LastIndexOf()
是 String
类的一个方法。
LastIndexOf() 参数
LastIndexOf()
方法接受以下参数:
- value - 要查找的子字符串
- startIndex - 搜索的起始位置。搜索从
startIndex
开始,向给定的字符串的开头方向进行。 - count - 要检查的字符位置数。
- comparisonType - 枚举值,指定搜索的规则。
LastIndexOf() 返回值
- 返回指定字符/字符串的最后一次出现的索引
- 如果未找到指定字符/字符串,则返回**-1**。