2013-04-08 58 views
-1

我有一個像這樣的字符串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串那就是1234ABCD1A我想在循環中獲得第三個值。所以我想到了使用substring而不是使用split。

輸出應該是:

first time: 1234ABCD

second time: 1A

third time: AB

+0

你編輯的問題不明確。你想達到什麼目的? – 2013-04-08 10:40:53

回答

0

我只是錯過了指數

string tempVar = test.Substring(0, test.IndexOf(separator[0].ToString())); 
2

使用Split函數:

string s = "1234ABCD-1A-AB"; 
string[] parts = s.Split('-'); 

則:

s[0] == "1234ABCD" 
s[1] == "1A" 
s[2] == "AB" 

基於現在更新的要求,請嘗試以下操作:

string input = "1234ABCD-1A-AB"; 
char separator = '-'; 

string[] parts = input.Split(separator); 

// if you do not need to know the item index: 
foreach (string item in parts) 
{ 
    // do something here with 'item' 
} 

// if you need to know the item index: 
for (int i = 0; i < parts.Length; i++) 
{ 
    // do something here with 'item[i]', where i is 
    // the index (so 1, 2, or 3 in your case). 
} 
0
string[] items = str.Split(new char[] { '-' }); 
+0

如果我將使用split,那麼我將如何獲得第三個值,如果我將傳遞循環索引'string temp = test1 [i] .ToString(); – Rocky 2013-04-08 10:25:40

+0

我已經給出了一些解釋,請檢查 – Rocky 2013-04-08 10:49:05

+0

還不清楚。 嘗試發佈您的輸入數據和您的整個代碼。 和「'」+分隔符+「'」({「'」}部分)的處理是什麼? – 2013-04-08 11:07:33

0

您可以使用String.Split方法

返回包含子在一個字符串數組此實例 由指定字符串的元素分隔o r Unicode 字符數組。

string s = "1234ABCD-1A-AB"; 
string[] items = s.Split('-'); 

for(int i = 0; i < items.Length; i++) 
    Console.WriteLine("Item number {0} is: {1}", i, items[i]); 

輸出將是;

Item number 0 is: 1234ABCD 
Item number 1 is: 1A 
Item number 2 is: AB 

這是DEMO

2

您可以使用Split和您的分隔符'-',然後訪問返回的string[]

string[] parts = test.Split('-'); 
string firstPart = parts[0]; 
string secondPart = parts.ElementAtOrDefault(1); 
string thirdPart = parts.ElementAtOrDefault(2); 

Demo

+0

+1好極了,從未想過這樣做 – Steve 2013-04-08 10:27:30

+0

Woharey!我從來沒有使用'ElementAtOrDefault'。 Tim很酷! +1當然.. – 2013-04-08 10:28:09

+0

@Tim:請檢查我的更新qus。 – Rocky 2013-04-08 10:32:17

0

很簡單經由String.Split()

string t = "1234ABCD-1A-AB"; 
string[] tempVar = t.Split('-'); 

foreach(string s in tempVar) 
{ 
    Console.WriteLine(s); 
} 
Console.Read(); 

打印:

1234ABCD 1A AB

+0

請檢查我更新的問題 – Rocky 2013-04-08 10:40:55

+0

Downvoter care to comment? @Rocky您需要清楚地佈置您的需求。 – DGibbs 2013-04-08 11:03:13

0

您可以使用String.Split()

string str = "1234ABCD-1A-AB"; 
string[] splitted = str.Split('-'); 
/* foreach (string item in splitted) 
{ 
    Console.WriteLine(item); 
}*/ 

,你可以將其設置爲:

string firstPart = splitted.FirstOrDefault(); 
string secondPart = splitted.ElementAtOrDefault(1); 
string thirdPart = splitted.ElementAtOrDefault(2);