“1654654------1222”或“1465465------25”等数据只获取右边后面数字的C#代码
时间: 2024-05-31 09:05:42 浏览: 9
你可以使用 C# 中的 Substring 方法来获取右边的数字。例如,假设你的字符串为 str,右边数字的长度为 n,那么可以使用以下代码:
```csharp
string rightDigits = str.Substring(str.Length - n);
```
其中,str.Length 表示字符串 str 的长度,str.Length - n 表示右边数字的起始位置,Substring 方法则从该位置开始截取字符串,并返回右边的数字。
如果你想获取多个字符串中的右边数字,可以将以上代码放在一个循环中,并对每个字符串执行一次。
相关问题
------1222或------25等数据只获取后面数字的C#代码
获取字符串中的数字可以使用正则表达式进行匹配,可以使用以下C#代码实现:
```
string input = "1222";
string pattern = @"(\d+)$";
Match match = Regex.Match(input, pattern);
if (match.Success)
{
string result = match.Groups.Value;
Console.WriteLine(result);
}
```
该代码使用了正则表达式`@"(\d+)$"`,表示匹配字符串末尾的一个或多个数字。使用`Regex.Match()`方法进行匹配,如果匹配成功则从`Match`对象中获取匹配的结果,即最后的数字部分。
C#------获取最后一个
如果你是要获取一个字符串中的最后一个字符,可以使用以下代码:
```csharp
string str = "Hello World!";
char lastChar = str[str.Length - 1];
Console.WriteLine(lastChar); // 输出 "!"
```
如果你是要获取一个集合中的最后一个元素,可以使用以下代码:
```csharp
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
int lastNumber = numbers[numbers.Count - 1];
Console.WriteLine(lastNumber); // 输出 5
```
如果你是要获取一个数组中的最后一个元素,可以使用以下代码:
```csharp
int[] numbers = new int[] { 1, 2, 3, 4, 5 };
int lastNumber = numbers[numbers.Length - 1];
Console.WriteLine(lastNumber); // 输出 5
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)
![](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)