📅  最后修改于: 2020-10-31 03:43:19             🧑  作者: Mango
C#LastIndexOf()方法用于查找指定字符在String中最后一次出现的索引位置。
public int LastIndexOf(Char ch)
public int LastIndexOf(Char, Int32)
public int LastIndexOf(Char, Int32, Int32)
public int LastIndexOf(String)
public int LastIndexOf(String, Int32)
public int LastIndexOf(String, Int32, Int32)
public int LastIndexOf(String, Int32, Int32, StringComparison)
public int LastIndexOf(String, Int32, StringComparison)
public int LastIndexOf(String, StringComparison)
ch:它是一个字符类型参数,用于查找字符串中给定字符的最后一次出现。
它返回整数值。
using System;
public class StringExample
{
public static void Main(string[] args)
{
string s1 = "Hello C#";
int index = s1.LastIndexOf('l');
Console.WriteLine(index);
}
}
输出:
3
IndexOf()方法返回第一个匹配字符的索引号,而LastIndexOf()方法返回最后一个匹配字符的索引号。
using System;
public class StringExample
{
public static void Main(string[] args)
{
string s1 = "Hello C#";
int first = s1.IndexOf('l');
int last = s1.LastIndexOf('l');
Console.WriteLine(first);
Console.WriteLine(last);
}
}
输出:
2
3