想从我的数组中检索字符串 [英] Want To retrieve string from my array
本文介绍了想从我的数组中检索字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的字符串数组包含以下值
空
串1
串2
字串3
空
字串4
字串5
字串6
空
字串7
串8
串9
我想找回
字符串1字符串2字符串3
字串4字串5字串6
字串7串8串9
解决方案
一个小LINQ魔术
VAR ARR =新[] {NULL,字符串1,字符串2,STRING3,NULL,串,,STRING5,string6,NULL,string7,string8,string9};
VAR项目= arr.Select((项指数)=>新建项目{,nextNull = Array.IndexOf(ARR,空,指数)})
.GroupBy(X = > x.nextNull)
。凡(X => x.Any(Y =>!String.IsNullOrEmpty(y.item)))
。选择(X =>的string.join (,x.Select(Y => y.item)));
输出:
字符串1字符串2 STRING3
串,STRING5 string6
string7 string8 string9
活生生的例子: http://rextester.com/GOIT12899
My string array contains below values
null
String 1
String 2
String 3
null
String 4
String 5
String 6
null
String 7
String 8
String 9
I want to retrieve
String 1 String 2 String 3
String 4 String 5 String 6
String 7 String 8 String 9
解决方案
A little LINQ magic
var arr = new [] {null,"string1","string2","string3",null,"string4","string5","string6",null,"string7","string8","string9"};
var items = arr.Select( (item,index) => new {item, nextNull = Array.IndexOf(arr,null,index)} )
.GroupBy(x => x.nextNull)
.Where(x => x.Any(y => !String.IsNullOrEmpty(y.item)))
.Select(x => String.Join(" ",x.Select(y => y.item)));
Output:
string1 string2 string3
string4 string5 string6
string7 string8 string9
Live example: http://rextester.com/GOIT12899
这篇关于想从我的数组中检索字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文