我有一個像這樣的字符串1234ABCD-1A-AB
我有分隔符在字符串[]分隔符,我循環直到字符串的長度。我想獲得substring
。在循環中我寫下面的代碼如何獲取子字符串
string tempVar = test.Substring(0, test.IndexOf("'" + separator+ "'"));
我想這樣也
string tempVar = String.Join(",", test.Split(',').Select(s => s.Substring(0, s.IndexOf("'" + separator+ "'"))));
使用此我得到錯誤索引不應該小於0,循環將只運行2次,因爲我是基於分隔符的循環,並且在我的字符串中有2個分隔符。
讓我解釋一下:
我有隔板的循環,因爲我會2分隔一個是第9位,和其他一個將執行僅2時間:放置14日,即循環中,我根據分割字符串在我的下一個步驟分離
string[] test1 = test.Split("'" + separator+ "'");
我傳遞一個字符串值下道工序是這樣
string temp = test1[i].ToString();
與這個我只得到2串那就是1234ABCD
和1A
我想在循環中獲得第三個值。所以我想到了使用substring而不是使用split。
輸出應該是:
first time: 1234ABCD
second time: 1A
third time: AB
你編輯的問題不明確。你想達到什麼目的? – 2013-04-08 10:40:53