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

c# 取字符串最后一个字符

作者:伤哖铱旧   发布日期:2025-07-07   浏览:81

// 获取字符串的最后一个字符
string input = "Hello, World!";
if (input.Length > 0)
{
    char lastChar = input[input.Length - 1];
    Console.WriteLine("最后一个字符是: " + lastChar);
}
else
{
    Console.WriteLine("字符串为空");
}

解释说明:

  1. input 是一个字符串变量,包含我们要处理的字符串。
  2. 使用 input.Length 获取字符串的长度,并检查是否大于 0,以确保字符串不为空。
  3. 如果字符串不为空,则使用 input[input.Length - 1] 获取最后一个字符。因为字符串索引是从 0 开始的,所以最后一个字符的索引是 input.Length - 1
  4. 最后一个字符被存储在 lastChar 变量中,并通过 Console.WriteLine 输出结果。
  5. 如果字符串为空,则输出提示信息 "字符串为空"。

上一篇:c# if else

下一篇:c# rectangle

大家都在看

c# 二进制

c# tcp client

c# type.gettype

c# sqlconnection

c# string.format 小数位数

.net和c#

c#游戏开发

c#网络编程

c# rectangle

c# 取字符串最后一个字符

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

Laravel 中文站