2010-07-15 56 views
7

我有一个字符串,象这样:C#字符串分割 - 破串起来在第二逗号

mystring = "test1, 1, anotherstring, 5, yetanother, 400"; 

myarray中可以是不同长度的。我想要做的是将字符串拆分为:

{"test1, 1"} 
{"anotherstring, 5} 
{"yetanother, 400"} 

这可能吗?我尝试了string[] newArray = mystring.Split(','),但是它以每个逗号分割,而不是我想要做的第二个逗号。

感谢您的帮助

的ZAP

回答

8

您可以使用正则表达式在字符串中匹配两个项目:

string[] parts = 
    Regex.Matches(myarray[0], "([^,]*,[^,]*)(?:, |$)") 
    .Cast<Match>() 
    .Select(m => m.Groups[1].Value) 
    .ToArray(); 

这从阵列中的第一个字符串获取项目。我不知道为什么你有一个数组中的字符串,如果你有多个字符串,在这种情况下,你必须遍历它们并从每个字符串中获取项目。

+0

我喜欢这个。我认为它是最可读的(即没有数学),并且最不容易被一个错误等。+1 – spender 2010-07-15 10:43:14

+0

一个比较简单的正则表达式是@“\ w +,\ s + \ w +” – 2010-07-15 10:48:05

+0

@Jim:是的,它适用于特定字符串,但是例如用'“test,1,lost + found,2”或test“,1,2”'失败。 – Guffa 2010-07-15 10:53:28

1

不单独Split,但它一定能够实现。

我认为myarray实际上是一个字符串,而不是一个数组... ...?

在这种情况下,你也许可以做这样的事情:

string myarray = "test1, 1, anotherstring, 5, yetanother, 400"; 

string[] sourceArray = myarray.Split(','); 
string[] newArray = sourceArray.Select((s,i) => 
    (i % 2 == 0) ? "" : string.Concat(sourceArray[i-1], ",", s).Trim() 
).Where(s => !String.IsNullOrEmpty(s)).ToArray(); 
+0

道歉 - 是myArray的是一个字符串。我会更新 – 2010-07-15 10:38:54

4

有使String.Split这样做的直接方式。

如果性能是不是一个问题,你可以使用LINQ:

var input = "test1, 1, anotherstring, 5, yetanother, 400"; 

string[] result = input.Split(','); 
result = result.Where((s, i) => i % 2 == 0) 
       .Zip(result.Where((s, i) => i % 2 == 1), (a, b) => a + ", " + b) 
       .ToArray(); 

否则你很有可能要分开使用String.IndexOf手动或使用正则表达式的字符串。

+0

+1这个问题,因为'Zip'非常出色。这只会在4.0+寿。 – 2010-07-15 10:44:43

1

您可能可以使用原始字符串上的正则表达式来替换每个其他逗号,并使用不同的“标记”,例如, ';' 然后只需调用新标记上的string.split。

2

这里是另一个基于LINQ的解决方案。 (也许不是最高效的,但它允许简洁的代码和工作分组到任意组大小)。


1)定义一个新的查询操作,InGroupsOf

public static IEnumerable<T[]> InGroupsOf<T>(this IEnumerable<T> parts, 
              int groupSize) 
{ 
    IEnumerable<T> partsLeft = parts; 
    while (partsLeft.Count() >= groupSize) 
    { 
     yield return partsLeft.Take(groupSize).ToArray<T>(); 
     partsLeft = partsLeft.Skip(groupSize); 
    } 
} 

2)其次,它适用于您的输入:

// define your input string: 
string input = "test1, 1, anotherstring, 5, yetanother, 400"; 

// split it, remove excessive whitespace from all parts, and group them together: 
IEnumerable<string[]> pairedInput = input 
            .Split(',') 
            .Select(part => part.Trim()) 
            .InGroupsOf(2); // <-- used here! 

// see if it worked: 
foreach (string[] pair in pairedInput) 
{ 
    Console.WriteLine(string.Join(", ", pair)); 
} 
0

有趣的问题...我会这样做:

string input = "test1, 1, anotherstring, 5, yetanother, 400"; 
string pattern = @"([^,]*,[^,]*),"; 

string[] substrings = Regex.Split(input, pattern).Where(s => s!="").Select(s => s.Trim()).ToArray(); 

你得到了你想要的。只有它的脏...= P =)

0

使用的IEnumerator ..

 var myarray = "test1, 1, anotherstring, 5, yetanother, 400"; 
     System.Collections.IEnumerator iEN = myarray.Split(',').GetEnumerator(); 
     var strList = new List<string>(); 
     while (iEN.MoveNext()) 
     { 
      var first = iEN.Current; 
      iEN.MoveNext(); 
      strList.Add((string)first + "," + (string)iEN.Current); 
     } 

:)

相关问题