C# 获取字符串的最后一位字符
可以使用 'Substring' 方法来获取字符串的最后一位作为子字符串。可以使用以下代码实现:
string str = 'Hello World!';
string subStr = str.Substring(str.Length - 1);
Console.WriteLine(subStr); // 输出 '!'
// 或者可以使用下面的代码来获取字符串的最后一位作为字符
char lastChar = str[str.Length - 1];
Console.WriteLine(lastChar); // 输出 '!'
在这个例子中,我们定义了一个字符串 'str',然后使用 'Substring' 方法来获取字符串的最后一位字符。另外,我们也可以直接通过索引来获取字符串的最后一位字符。
原文地址: https://www.cveoy.top/t/topic/p09u 著作权归作者所有。请勿转载和采集!