Linux 拨号vps windows公众号手机端

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

lewis 5年前 (2020-01-09) 阅读数 11 #程序编程
文章标签 c#

在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");
}

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

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门