如何在C#中获取除最后一个元素之外的所有数组元素 [英] how to take all array elements except last element in C#
本文介绍了如何在C#中获取除最后一个元素之外的所有数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样的字符串数组.
I have a string array like this.
string[] 查询
数据不止一个字符串.
string[] queries
with data more than one string.
我想跳过元素中的最后一个字符串并获取剩余的字符串.我想出了
I want to skip the last string from the element and take the remaining. I have come up with
var remStrings = queries.Reverse().Skip(1).Take(queries.Length - 1);
有没有更好的替代方法?
Is there a better alternative to this?
推荐答案
var remStrings = queries.Take(queries.Length - 1);
无需反转和跳过.只需比数组中的元素少一个.
No need to Reverse and Skip. Just take one less element than there are in the array.
如果您真的希望元素按相反的顺序排列,您可以在末尾添加 .Reverse()
.
If you really wanted the elements in the reverse order, you could tack on a .Reverse()
to the end.
这篇关于如何在C#中获取除最后一个元素之外的所有数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文