温馨提示×

C#中搜索字符串的方法是什么

c#
小亿
351
2024-04-29 09:22:44
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在C#中搜索字符串可以使用IndexOf方法或者Contains方法。

  1. 使用IndexOf方法:
string str = "Hello, world!";
int index = str.IndexOf("world");
if(index != -1)
{
    Console.WriteLine("String found at index: " + index);
}
else
{
    Console.WriteLine("String not found");
}
  1. 使用Contains方法:
string str = "Hello, world!";
if(str.Contains("world"))
{
    Console.WriteLine("String found");
}
else
{
    Console.WriteLine("String not found");
}

这两种方法都可以用来搜索字符串,并返回相应的结果。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:C#字符串拼接的方法是什么

0