Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c# indexof

作者:浪推晚风   发布日期:2025-10-25   浏览:70

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

大家都在看

c# 二进制

c# tcp client

c# type.gettype

c# sqlconnection

c# string.format 小数位数

.net和c#

c#获取系统时间

c#游戏开发

c#网络编程

c# rectangle

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站