C # String splitting - line break in second comma

I have a line like:

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

myarray may have different lengths. What I would like to do is break the line like this:

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

Is it possible? I tried string[] newArray = mystring.Split(',') , but split it into each comma, not the second comma, which I would like to do.

thanks for the help

wants to give Petersburg

+7
string split c #
source share
7 answers

You can use regex to match two elements in a string:

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

Gets items from the first line of the array. I don’t know why you have a row in the array, and if you have several rows, in this case you will have to iterate over them and get elements from each row.

+8
source share

There is no direct way to force String.Split to do this.

If performance is not a concern, you can use 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(); 

Otherwise, you may have to split the string manually using String.IndexOf or using a regular expression.

+4
source share

Another LINQ-based solution. (Perhaps not the most efficient, but it allows you to get compressed code and works to group into arbitrary group sizes).


1) Define a new query statement, 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) Secondly, apply it to your input:

 // 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)); } 
+2
source share

Only with Split , but this can certainly be achieved.

I suppose myarray is actually a string, not an array ...?

In this case, you can do something like this:

 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(); 
+1
source share

You can probably use the regular expression in the source line to replace each other with a different β€œtoken”, for example. ';' Then just call string.split instead of the new token.

+1
source share

An interesting question ... I would do it like this:

 string input = "test1, 1, anotherstring, 5, yetanother, 400"; string pattern = @"([^,]*,[^,]*),"; string[] substrings = Regex.Split(input, pattern).Where(s => s!="").Select(s => s.Trim()).ToArray(); 

You get exactly what you want. Only its dirty ... = P =)

0
source share

using 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); } 

:)

0
source share

All Articles