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

c#查找字符串中指定字符的位置

作者:寂寞算什么   发布日期:2026-07-07   浏览:103

using System;

class Program
{
    static void Main()
    {
        string str = "Hello, world!";
        char targetChar = 'w';

        // 使用IndexOf方法查找指定字符的位置
        int position = str.IndexOf(targetChar);

        if (position != -1)
        {
            Console.WriteLine($"字符 '{targetChar}' 在字符串中的位置是: {position}");
        }
        else
        {
            Console.WriteLine($"字符 '{targetChar}' 不在字符串中");
        }
    }
}

解释说明:

  • str 是我们要查找的字符串。
  • targetChar 是我们想要查找的字符。
  • IndexOf 方法用于查找指定字符在字符串中的第一个出现位置。如果找到了,它返回该字符的索引(从0开始);如果没有找到,则返回 -1
  • 如果找到了字符,程序会输出该字符的位置;否则,会提示字符不在字符串中。

上一篇:c# crc16

下一篇:c# byte转int

大家都在看

c# 二进制

c# 创建目录

c# socket服务端连接多个客户端

c# datatable group by

c# tcp client

c# type.gettype

c# sqlconnection

c# string.format 小数位数

c# invoke方法

.net和c#

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

Laravel 中文站