2013-03-16 121 views
0

我一直試圖分裂一個字符串兩次,但我不斷收到錯誤「索引超出了數組的界限」。如何分割一個字符串TWICE

這是我打算分割字符串:

"a*b*c*d*e^1*2*3*4*5^e*f*g*h*i^" 

使得我使用"^"作爲分隔符在第一陣列分離,使得每個組將尋找第一結果後如下

a*b*c*d*e 1*2*3*4*5 e*f*g*h*i 

然後此後對這個組與*執行另一個分割操作作爲隔膜,使得結果,例如,從所述第一組是a b c d e

這是C#代碼:

words = "a*b*c*d*e^1*2*3*4*5^e*f*g*h*i^"; 

char[] del = { '^' }; 

string[] splitResult = words.Split(del); 
foreach (string w in splitResult) 
{ 
    char[] separator = { '*' }; 
    string[] splitR = w.Split(separator); 
    foreach (string e in splitR) 
    { 
     string first = splitR[0]; 
     string second = splitR[1]; 
     string third = splitR[2]; 
     string fourth = splitR[3]; 
     string fifth = splitR[4]; 
    } 
} 
+1

你有沒有試過words.Split(del,StringSplitOptions.RemoveEmptyEntries);?第一次拆分後的輸出是否爲空? – David 2013-03-16 07:39:39

+2

您應該檢查splitR-array是否包含5個預期的元素。問題是最後一個w不包含任何數據(因爲最後一個'^') – Casperah 2013-03-16 07:40:38

+0

@David謝謝,StringSplitOptions.RemoveEmptyEntries是我丟失的 – user2176410 2013-03-16 07:48:04

回答

1

你可以使用LINQ做到這一點:

IEnumerable<IEnumerable<string>> strings = words 
    .Split(new char[] { '^' }, StringSplitOptions.RemoveEmptyEntries) 
    .Select(w => w.Split('*')); 

,或者如果你喜歡使用數組專門工作

string[][] strings = words 
    .Split(new char[] { '^' }, StringSplitOptions.RemoveEmptyEntries) 
    .Select(w => w.Split('*').ToArray()) 
    .ToArray(); 
0
string words= "a*b*c*d*e^1*2*3*4*5^e*f*g*h*i^"; 
string[] reslts = words.Split(new char[] { '*', '^' }, StringSplitOptions.RemoveEmptyEntries); 
0

你有一個終止分隔符,所以最後的字符串是空的。

If (w != null) {     
string[] splitR = w.Split(separator); 
     If splitR.lenght > 4) 
     { 
       string first = splitR[0]; 
       string second = splitR[1]; 
       string third = splitR[2]; 
       string fourth = splitR[3]; 
       string fifth = splitR[4]; 
    } 
} 
4

要刪除那裏是沒有結果的最後一部分,如何

在C#

string str = "a*b*c*d*e^1*2*3*4*5^e*f*g*h*i^"; 
var result = str.Split(new char[] { '^' }, StringSplitOptions.RemoveEmptyEntries) 
       .Select(x => x.Split('*')).ToArray(); 

在VB.Net

Dim str As String = "a*b*c*d*e^1*2*3*4*5^e*f*g*h*i^" 
Dim result = str.Split(New Char() {"^"}, StringSplitOptions.RemoveEmptyEntries) 
       .Select(Function(x) x.Split("*")).ToArray() 
+0

+1,修正var result中的單引號= d.Split('^')。Select(x => x。分裂('*')); – 2013-03-16 07:51:06

+0

@romanm:完成謝謝。 – 2013-03-16 07:51:47

+0

也可能.ToArray()給他他想要的東西 – 2013-03-16 07:53:27

0

你得到例外Index was outside the bounds of the array因爲在上一個循環中,它只能獲得一個項目,我建議您檢查五個項目:

words = "a*b*c*d*e^1*2*3*4*5^e*f*g*h*i^"; 
char[] del = { '^' }; 

string[] splitResult = words.Split(del); 
foreach (string w in splitResult) 
{ 
    char[] separator = { '*' }; 
    string[] splitR = w.Split(separator); 
    if (splitR.Length>=5) 
    { 
     foreach (string e in splitR) 
     { 
      string first = splitR[0]; 
      string second = splitR[1]; 
      string third = splitR[2]; 
      string fourth = splitR[3]; 
      string fifth = splitR[4]; 
     } 
    } 
} 
0

試試這個:

string words = "a*b*c*d*e^1*2*3*4*5^e*f*g*h*i^"; 

char[] del = { '^' }; 

string[] splitResult = words.Split(del,StringSplitOptions.RemoveEmptyEntries); 
foreach (string w in splitResult) 
{ 
    char[] separator = { '*' }; 
    string[] splitR = w.Split(separator); 
    if(splitR.Length==5) 
    { 
     string first = splitR[0]; 
     string second = splitR[1]; 
     string third = splitR[2]; 
     string fourth = splitR[3]; 
     string fifth = splitR[4]; 

     Console.WriteLine("{0},{1},{2},{3},{4}", first, second, third, fourth, fifth); 
    } 
} 
0

一號線所有這一切

var f = words.Split(new char[] { '^' }, StringSplitOptions.RemoveEmptyEntries) 
      .Select(x => x.Split(new char[] { '*' }).ToArray()) 
      .ToArray(); 

你的第二個循環做5次同樣的事情(你不使用e)。

你得到的異常是因爲最後一個空字符串被包含而導致一個空數組在內循環中給出超出範圍的異常。