我有一個字符串:字符串數組
id0:xxxxx:id0-value:xxxxx:id1:xxxxxxxx:id1-value:xxxxx:id3:xxxxxxxx:id3-value:xxx
我只需要從字符串到數組IDX-value的值。
我該如何實現它?
我有一個字符串:字符串數組
id0:xxxxx:id0-value:xxxxx:id1:xxxxxxxx:id1-value:xxxxx:id3:xxxxxxxx:id3-value:xxx
我只需要從字符串到數組IDX-value的值。
我該如何實現它?
最簡單的方法,該值是在位置(4倍 - 1):
var list = input.Split(':');
var outputs = new List<string>();
for (int index = 0; index < list.Count(); index++)
{
if (index % 4 == 3)
outputs.Add(list.ElementAt(index));
}
使用正則表達式(一種正則表達式:x之後出現的正則表達式)進行拆分,然後使用冒號:
進行拆分,並使用第一個索引作爲Dictionary Key和第二個索引作爲Dictionary值。
我不推薦RegEx的原因是因爲數據看起來是在一個設定的模式中RegEx編譯和使用是昂貴的bo相比於只分割字符串而言,CPU和內存方面更爲出色。如果數據不是清晰的模式,那麼正則表達式就是要走的路。 – 2012-08-16 03:37:53
使用String.Split()
http://msdn.microsoft.com/en-us/library/system.string.split.aspx
String myString = "id0:xxxxx:id0-value:xxxxx:id1:xxxxxxxx:id1-value:xxxxx:id3:xxxxxxxx:id3-value:xxx";
String[] tokens = myString.Split(new Char[] {':'});
令牌數組將包含{ 「ID0」, 「XXXXX」, 「ID0-值」, 「XXXXX」, 「ID1」, 「xxxxxxxx」,「id1-value」,「xxxxx」,「id3」,「xxxxxxxx」,「d3-value」,「xxx」}
第二種可能性是使用String.IndexOf()和String。子串()。
http://msdn.microsoft.com/en-us/library/5xkyx09y http://msdn.microsoft.com/en-us/library/aka44szs
詮釋開始= 0; ArrayList令牌; ((start = myString.IndexOf(「 - value:」,start))> -1){ ArrayList.Add(myString.Substring(start + 6,myString.IndexOf(「:」,start + 7); 啓動+ = 6; //跳轉過去我們剛發現 }
家庭作業 – dthorpe 2012-08-16 03:27:40
嘗試新鮮事物或完全? – nunespascal 2012-08-16 03:27:42
一些正則表達式的好地方!一個字...匹配集合 – CoderMarkus 2012-08-16 03:28:48