2013-04-08 29 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); 
相关问题