Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a string on comma and space at the same time

Tags:

People also ask

How do you split a string with spaces and commas?

To split a string by space or comma, pass the following regular expression to the split() method - /[, ]+/ . The method will split the string on each occurrence of a space or comma and return an array containing the substrings.

How do you put a space in a split string?

To split a string with space as delimiter in Java, call split() method on the string object, with space " " passed as argument to the split() method. The method returns a String Array with the splits as elements in the array.

How do you split a space and comma in Python?

Python, split to split a comma-separated string, remove whitespace and convert to a list. When splitting a comma-separated string into a list in Python, if there are no spaces in between, just split() will work. If there are spaces, it is useful to combine it with strip() to remove the extra spaces.


I have a string which contains comma and space. I need to split this string based on these two and add it into a string array. Until now I have done it on either comma or space but not both at the same time.
Here is the string:

2013/02/05 11:50:57,00:00:17,5,9870,O,9851,9851,,1,1029441,0,E9870,Extn9870,E9851,GM PS,0,0,,,,,,,,,,,,,

As we can see in the above string example 2013/02/05 11:50:57 contains space and rest each one are separated by comma.

Here is the code that I have tried..

 string[] str = line.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);