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

   2024-10-20 7670
核心提示:在C#中搜索字符串可以使用IndexOf方法或者Contains方法。使用IndexOf方法:string str = Hello, world!;int index = str.IndexOf

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

使用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");}
使用Contains方法:
string str = "Hello, world!";if(str.Contains("world")){    Console.WriteLine("String found");}else{    Console.WriteLine("String not found");}

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

 
举报打赏
 
更多>同类网点查询
推荐图文
推荐网点查询
点击排行

网站首页  |  关于我们  |  联系方式网站留言    |  赣ICP备2021007278号