using System;
class Program
{
static void Main()
{
string text = "Hello, world!";
string substring = "world";
// IndexOf 方法用于查找子字符串在主字符串中的索引位置
int index = text.IndexOf(substring);
if (index != -1)
{
Console.WriteLine($"Substring '{substring}' found at index: {index}");
}
else
{
Console.WriteLine("Substring not found.");
}
}
}
IndexOf 方法用于查找指定子字符串在主字符串中第一次出现的位置。如果找到该子字符串,则返回其起始索引;否则返回 -1。text.IndexOf(substring) 会返回子字符串 "world" 在字符串 "Hello, world!" 中的位置,即索引 7。IndexOf 返回 -1,表示未找到该子字符串。上一篇:c# random
下一篇:c# tuple
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站