c#寻找字符串子字符串
时间: 2023-11-27 12:59:37 浏览: 97
在C#中,可以使用字符串的IndexOf方法来查找子字符串。例如,以下代码可以查找字符串“hello world”中是否包含子字符串“world”:
```
string str = "hello world";
int index = str.IndexOf("world");
if (index != -1)
{
Console.WriteLine("Substring found at index " + index);
}
else
{
Console.WriteLine("Substring not found");
}
```
注意,IndexOf方法返回子字符串的第一个字符的索引。如果子字符串不存在,则返回-1。
阅读全文
相关推荐











