1

I'm trying to split a string like this:

7       300685  1235    200017  200018  200019

In

7
300685
1235
200017
200018
200019

array of strings.

I've come up with this Regex but it keeps the white spaces too:

var myStrings = System.Text.RegularExpressions.Regex.Split(linea, @"\s+");

That's because I target any string that preceeds a white space. How to not to do that and keep only not white strings.

I know that it is easily done by removing empty strings from the array but I would like to do it with the regular expression.

Sturm
  • 3,968
  • 10
  • 48
  • 78
  • why don't you remove excess spaces before splitting the string? – Amber Mar 03 '14 at 09:21
  • 1
    why use regex for this? simply use the Split method on the string and remove empty entries. this is more than sufficient than doing a Regex - then you could theoretically just Trim() each item in the array – Ahmed ilyas Mar 03 '14 at 09:21
  • 4
    How many times this question has already been answered? I can't believe you haven't see the list of similar questions while you typed the title of this question – Steve Mar 03 '14 at 09:26

6 Answers6

6

Why don't you just use String.Split method ?

var myStrings = linea.Split(new [] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

Another way with Split and LINQ without RemoveEmptyEntries option:

var myStrings = linea.Split().Where(x => x.All(char.IsDigit)).ToArray();

Also there is a RegexOption.IgnorePatternWhitespace parameter that you can pass your Regex.Split method to remove whitespaces:

var myStrings = Regex.Split(linea, @"\s+", RegexOptions.IgnorePatternWhitespace);
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
6

You can use StringSplitOptions.RemoveEmptyEntries enumeration with String.Split method like;

The return value does not include array elements that contain an empty string

string s = "7       300685  1235    200017  200018  200019";
var array = s.Split(new []{" "}, StringSplitOptions.RemoveEmptyEntries);
foreach (var item in array)
{
    Console.WriteLine(item);
}

Output will be;

7
300685
1235
200017
200018
200019

Here a demonstration.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
5

I would suggest to simply use string.Split:

string[] s = yourString.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);

See MSDN.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
2

Why does this not work for you:

string str = "7       300685  1235    200017  200018  200019";
str.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);

You can also use String.Trim to remove all leading and trailing occurrences of a set of white-space characters.

Sadique
  • 22,572
  • 7
  • 65
  • 91
1

Try this

  string[] splitvalue = string.Split(str, ' ', StringSplitOptions.RemoveEmptyEntries);

or:

  string[] splitvalue = str.Split(null);

or:

string[] splitvalue = str.Split(new char[0]);
Vignesh Kumar A
  • 27,863
  • 13
  • 63
  • 115
0
var list = "Your String".Split(' ').Where(p =>!string.IsNullOrWhiteSpace(p));