[英]Split string and exclude last split
Is there anyway for me to split a string and exclude the last split?无论如何我可以拆分一个字符串并排除最后一个拆分吗?
My data looks like this: data1,data2,data3,
and so if I split the element the last element in the array will be empty, so I would just prefer to exclude it from the split.我的数据如下所示:
data1,data2,data3,
因此如果我拆分元素,数组中的最后一个元素将为空,因此我更愿意将其从拆分中排除。
Right now I have this:现在我有这个:
serialNumbers = delimitedSerials.ToString().Split(',');
Granted I know I can just leave it and in my for
loop just know to skip the last element, but was wondering if there was a simple way to just exclude it at the time of splitting.当然,我知道我可以保留它,并且在我的
for
循环中只知道跳过最后一个元素,但想知道是否有一种简单的方法可以在拆分时排除它。
您可以使用 StringSplitOptions 参数拆分它:
data.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
Below code has a Linq where clause to exclude the empty data下面的代码有一个 Linq where 子句来排除空数据
string input ="data1,data2,data3,";
var output = input.Split(',').Where(value => !string.IsNullOrEmpty(value));
foreach(string data in output)
Console.WriteLine(data);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.