在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");
}
这两种方法都可以用来搜索字符串,并返回相应的结果。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至 55@qq.com 举报,一经查实,本站将立刻删除。转转请注明出处:https://www.szhjjp.com/n/1011433.html