阅读量:3
在C#中,LastIndexOf方法用于查找一个字符串在另一个字符串中最后一次出现的索引
using System;
class Program
{
static void Main()
{
string str = "这是一个测试字符串,包含空字符串和一个重复的单词。";
string searchStr = "字符串";
int lastIndex = str.LastIndexOf(searchStr, StringComparison.OrdinalIgnoreCase);
if (lastIndex != -1)
{
Console.WriteLine($"找到 '{searchStr}' 在字符串 '{str}' 中最后一次出现的索引为: {lastIndex}");
}
else
{
Console.WriteLine($"在字符串 '{str}' 中未找到 '{searchStr}'");
}
}
}
在这个示例中,我们使用LastIndexOf方法查找子字符串"字符串"在目标字符串str中最后一次出现的索引。StringComparison.OrdinalIgnoreCase参数表示不区分大小写的比较。如果找到了子字符串,lastIndex将包含子字符串最后一次出现的索引;否则,它将等于-1。
以上就是关于“c# lastindexof 如何处理空字符串”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm