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
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站