// 获取字符串的最后一个字符
string input = "Hello, World!";
if (input.Length > 0)
{
char lastChar = input[input.Length - 1];
Console.WriteLine("最后一个字符是: " + lastChar);
}
else
{
Console.WriteLine("字符串为空");
}
解释说明:
input
是一个字符串变量,包含我们要处理的字符串。input.Length
获取字符串的长度,并检查是否大于 0,以确保字符串不为空。input[input.Length - 1]
获取最后一个字符。因为字符串索引是从 0 开始的,所以最后一个字符的索引是 input.Length - 1
。lastChar
变量中,并通过 Console.WriteLine
输出结果。上一篇:c# if else
下一篇:c# rectangle
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站