在C#中,可以使用字符串的IndexOf()方法来查找字符串中的子串。这个方法返回子串在原始字符串中第一次出现的位置索引,如果找不到子串则返回-1。
以下是一个示例代码:
string str = "Hello, World!";string subStr = "World";int index = str.IndexOf(subStr);if (index != -1){ Console.WriteLine($"子串在原始字符串中的位置索引为: {index}");}else{ Console.WriteLine("找不到子串.");}上面的代码会输出:子串在原始字符串中的位置索引为: 7,因为"World"子串在"Hello, World!"字符串中从索引7开始。


