我如何在C#中的字符串获得最后四个字符? [英] How do I get the last four characters from a string in C#?

查看:1022
本文介绍了我如何在C#中的字符串获得最后四个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个字符串:

 字符串的myString =34234234d124;

我要得到这个字符串,它是D124的最后四个字符。我可以使用,但它需要一对夫妇code线的。

是否有可能得到这样的结果在一个前pression用C#?


解决方案

  mystring.Substring(Math.Max​​(0,mystring.Length  -  4));这//多少行是什么?

如果你是积极的字符串的长度至少是4,那么它更短:

  mystring.Substring(mystring.Length  -  4);

Suppose I have a string:

string mystring = "34234234d124";

I want to get the last four characters of this string which is "d124". I can use SubString, but it needs a couple of lines of code.

Is it possible to get this result in one expression with C#?

解决方案

mystring.Substring(Math.Max(0, mystring.Length - 4)); //how many lines is this?

If you're positive the length of your string is at least 4, then it's even shorter:

mystring.Substring(mystring.Length - 4);

这篇关于我如何在C#中的字符串获得最后四个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆