获取字符串数组的第一项和最后一项 [英] Get the first and last item of an array of strings

查看:112
本文介绍了获取字符串数组的第一项和最后一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有以下字符串数组:

string[] stringArray = {"one", "two", "three", "four"};

除了使用数组索引(stringArray [0],stringArray [3])(类似于stringArray.First)之外,是否还有一种方法可以从数组中获取C#中的第一项和最后一项(一个",四个").和stringArray.Last吗?

谢谢.

解决方案

使用LINQ First() Last()方法.

此外,这两种方法都具有有用的重载功能,该功能允许为要被视为第一个或最后一个元素的元素指定布尔条件.

If I have the following array of strings:

string[] stringArray = {"one", "two", "three", "four"};

Is there a way to get the first and last item ("one", "four") in C# from the array, besides using array indexing (stringArray[0], stringArray[3]), something like stringArray.First and stringArray.Last ?

Thanks.

解决方案

Use LINQ First() and Last() methods.

Moreover, both methods have useful overload which allows specifying boolean condition for elements to be considered as first or last.

这篇关于获取字符串数组的第一项和最后一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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