C#数组得到最后一个项目,从分裂在一条线 [英] C# array get last item from split in one line

查看:121
本文介绍了C#数组得到最后一个项目,从分裂在一条线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个工程得到一个数组的第一个项目

 字符串ASTRING = @你好/所有的\这\是/ A \测试;
字符串firstItemOfSplit = aString.Split(新的char [] {'\\','/'})[0];
// firstItemOfSplit =你好
 

有没有办法让最后一个项目?类似于

 字符串ASTRING = @你好/所有的\这\是/ A \测试;
字符串lastItemOfSplit = aString.Split(新的char [] {'\\','/'})[index.last]。
// lastItemOfSplit =测试
 

解决方案

您可以使用IEnumerable.Last()从System.Linq的扩展方法。

 字符串lastItemOfSplit = aString.Split(新的char [] {@\[0],/[0]})最近()。
 

I know that this works to get the first item of an array

string aString = @"hello/all\this\is/a\test";
string firstItemOfSplit = aString.Split(new char[] {'\\', '/'})[0];
//firstItemOfSplit = hello

is there a way to get the last item? Something like

string aString = @"hello/all\this\is/a\test";
string lastItemOfSplit = aString.Split(new char[] {'\\', '/'})[index.last];
//lastItemOfSplit = test

解决方案

You can use IEnumerable.Last() extension method from System.Linq.

string lastItemOfSplit = aString.Split(new char[] {@"\"[0], "/"[0]}).Last();

这篇关于C#数组得到最后一个项目,从分裂在一条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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