假设我有一个字符串:
"34234234d124"
我想要得到这个字符串的最后四个字符,这个字符串是“d124”。我可以使用SubString,但它需要几行代码,包括命名一个变量。
是否有可能在c#的一个表达式中得到这个结果?
假设我有一个字符串:
"34234234d124"
我想要得到这个字符串的最后四个字符,这个字符串是“d124”。我可以使用SubString,但它需要几行代码,包括命名一个变量。
是否有可能在c#的一个表达式中得到这个结果?
当前回答
mystring.Substring(Math.Max(0, mystring.Length - 4)); //how many lines is this?
如果你确定字符串的长度至少是4,那么它甚至更短:
mystring.Substring(mystring.Length - 4);
其他回答
使用泛型Last<T>。这将适用于任何IEnumerable,包括string。
public static IEnumerable<T> Last<T>(this IEnumerable<T> enumerable, int nLastElements)
{
int count = Math.Min(enumerable.Count(), nLastElements);
for (int i = enumerable.Count() - count; i < enumerable.Count(); i++)
{
yield return enumerable.ElementAt(i);
}
}
和string的一个特定的:
public static string Right(this string str, int nLastElements)
{
return new string(str.Last(nLastElements).ToArray());
}
这对于任何长度的字符串都不会失败。
string mystring = "34234234d124";
string last4 = Regex.Match(mystring, "(?!.{5}).*").Value;
// last4 = "d124"
last4 = Regex.Match("d12", "(?!.{5}).*").Value;
// last4 = "d12"
对于当前的任务来说,这可能有些多余,但如果需要进行额外的验证,则可以将其添加到正则表达式中。
编辑:我认为这个正则表达式会更有效:
@".{4}\Z"
假设你想要一个距离最后一个字符10个字符的字符串之间的字符串,你只需要3个字符。
我们写入StreamSelected = "rtsp://72.142.0.230:80/ smile - chan -273/ 4cif -273.stream"
在上面,我需要提取我将在数据库查询中使用的“273”
//find the length of the string
int streamLen=StreamSelected.Length;
//now remove all characters except the last 10 characters
string streamLessTen = StreamSelected.Remove(0,(streamLen - 10));
//extract the 3 characters using substring starting from index 0
//show Result is a TextBox (txtStreamSubs) with
txtStreamSubs.Text = streamLessTen.Substring(0, 3);
我想扩展现有的答案,提到在c# 8或更高版本中使用新的范围:为了使代码可用于所有可能的字符串,甚至那些小于4的字符串,需要某种形式的条件!如果您想复制代码,我建议使用示例5或6。
string mystring ="C# 8.0 finally makes slicing possible";
1:切片取结尾部分-通过指定从开始省略多少字符-这是VS 2019的建议:
string example1 = mystring[Math.Max(0, mystring.Length - 4)..] ;
2:切片取结尾部分-通过指定从结尾取多少字符:
string example2 = mystring[^Math.Min(mystring.Length, 4)..] ;
3:切片取结束部分-通过用?:操作符替换Max/Min:
string example3 = (mystring.length > 4)? mystring[^4..] : mystring);
就我个人而言,我更喜欢第二种和第三种变体。
索引和范围的MS文档参考:
空吗?但是,关于普遍性,我们还没有完成。到目前为止,每个例子都会抛出null字符串的异常。考虑null(如果你在c# 8或更高版本中不使用不可空字符串),并且不使用'if'(经典示例'with if'已经在另一个答案中给出),我们需要:
4:切片考虑空-通过指定省略多少字符:
string example4 = mystring?[Math.Max(0, mystring.Length - 4)..] ?? string.Empty;
5:切片考虑空-通过指定多少字符采取:
string example5 = mystring?[^Math.Min(mystring.Length, 4)..] ?? string.Empty;
6:使用?:操作符(和另外两个'?'操作符;-): (你不能把它放在一个完整的字符串插值中,例如WriteLine。)
string example6 = (mystring?.Length > 4) ? filePath[^4..] : mystring ?? string.Empty;
7: c# 6或7.x的Substring()的等效变体: (你不能把它放在一个完整的字符串插值中,例如WriteLine。)
string example7 = (mystring?.Length > 4) ? mystring.Substring(mystring.Length- 4) : mystring ?? string.Empty;
优雅降级? 我喜欢c#的新特性。像上一个例子那样把它们放在一行可能看起来有点过分。我们最后有点perl ' ish,不是吗? 但它是一个学习的好例子,我可以在一个测试过的库方法中使用它一次。 更好的是,如果我们愿意,我们可以在现代c#中摆脱null,并避免所有这些特定于null的处理。
这样的库/扩展方法作为一个快捷方式是非常有用的。尽管c#很先进,但你必须自己编写代码,这样才能更容易使用,而不是重复上面的代码来处理每个小的字符串操作。
我是开始学习BASIC语言的人之一,40年前就已经有了Right$(,)。有趣的是,在VB和c#中仍然可以使用string . right(,),正如在另一个答案中所显示的那样。
c#选择了精确而不是优雅的退化(与旧的BASIC相反)。 所以在这些答案中复制任何你喜欢的合适的变体,并为自己定义一个优雅的快捷函数,我的是一个名为RightChars(int)的扩展函数。
使用Substring实际上是非常简短和可读的:
var result = mystring.Substring(mystring.Length - Math.Min(4, mystring.Length));
// result == "d124"