15

I need to split a string separated by multiple spaces. For example:

"AAAA AAA        BBBB BBB BBB        CCCCCCCC"

I want to split it into these:

"AAAA AAA"   
"BBBB BBB BBB"
"CCCCCCCC"

I tried with this code:

value2 = System.Text.RegularExpressions.Regex.Split(stringvalue, @"\s+");

But not success, I only want to split the string by multiple spaces, not by single space.

3
  • How many spaces could there be between items? Commented Jul 18, 2013 at 18:25
  • 1
    does it have to be done with RegEx? Commented Jul 18, 2013 at 18:27
  • 1
    They are separated by more than one space. It doesn't matter how many.. The point is to ignore the single space. Commented Jul 18, 2013 at 18:37

3 Answers 3

43

+ means "one or more", so a single space would qualify as a separator. If you want to require more than once, use {m,n}:

value2 = System.Text.RegularExpressions.Regex.Split( stringvalue, @"\s{2,}");

The {m,n} expression requires the expression immediately prior to it match m to n times, inclusive. Only one limit is required. If the upper limit is missing, it means "m or more repetitions".

Sign up to request clarification or add additional context in comments.

Comments

2
value2 = System.Text.RegularExpressions.Regex.Split( stringvalue, @"\s{2,}");

Comments

2
value2 = System.Text.RegularExpressions.Regex.Split( stringvalue, @"\s\s+");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.