如何采取在C#中,除了最后一个元素,所有数组元素 [英] how to take all array elements except last element in C#

查看:290
本文介绍了如何采取在C#中,除了最后一个元素,所有数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串数组是这样的。

的String []查询数据超过一个字符串。

我想跳过从元素最后一个字符串,并采取剩余。我想出了

  VAR remStrings = queries.Reverse()跳过(1)。取(queries.Length  -  1)。

有没有更好的替代品呢?


解决方案

  VAR remStrings = queries.Take(queries.Length  -  1);

没有需要扭转,蹦跳。只要看看少了一个元素以外还有数组中

如果你真的想要以相反的顺序的元素,可以钉在一个 .Reverse()来结束。

I have a string array like this.

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.

If you really wanted the elements in the reverse order, you could tack on a .Reverse() to the end.

这篇关于如何采取在C#中,除了最后一个元素,所有数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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