2013-03-16 122 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)。

你得到的异常是因为最后一个空字符串被包含而导致一个空数组在内循环中给出超出范围的异常。