c#截取字符串最后一位的语句
时间: 2023-07-17 18:03:37 浏览: 61
在C#中,可以使用`Substring()`方法来截取字符串的最后一位。下面是一个示例:
```csharp
string str = "Hello";
string lastChar = str.Substring(str.Length - 1);
Console.WriteLine(lastChar); // 输出: o
```
在上面的代码中,我们定义了一个字符串`str`,然后使用`Substring()`方法来截取最后一位字符。`str.Length - 1`表示字符串的最后一个索引位置,然后通过`Substring()`方法将该位置的字符截取出来。
请注意,`Substring()`方法返回一个新的字符串,包含了所截取的字符。这样你就可以在后续的代码中使用这个截取的字符了。
相关问题
C#截取字符串第一位
以下是使用C#截取字符串第一位的两种方法:
1. 使用Substring方法:
```csharp
string str = "GTAZB_JiangjBen_123";
string firstChar = str.Substring(0, 1);
Console.WriteLine(firstChar); // 输出:G
```
2. 使用索引访问:
```csharp
string str = "GTAZB_JiangjBen_123";
char firstChar = str[0];
Console.WriteLine(firstChar); // 输出:G
```
c#截取字符串最后几个的字符
可以使用 `Substring` 方法和 `Length` 属性来截取字符串最后几个的字符。
示例代码如下:
```csharp
string originalString = "Hello, world!";
int length = 6;
if (length <= originalString.Length)
{
string substring = originalString.Substring(originalString.Length - length);
Console.WriteLine(substring);
}
```
这段代码会输出 `world!`,也就是在原字符串中从末尾开始数的最后6个字符。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)