6

Something like: How to split String with some separator but without removing that separator in Java?

I need to take "Hello World" and get ["Hello", " ", "World"]

Community
  • 1
  • 1
LunchMarble
  • 5,079
  • 9
  • 64
  • 94
  • 6
    Why? Why can't you just assume that there is a space between every two elements? – SLaks Nov 20 '11 at 03:17
  • 1
    Why exactly? You know that the separator is a space, so for each element of the resulting array there would have been a space between the contiguous elements. If you use string.Join() you can put the space back in. I'm puzzled as to why you need it in the split results. – slugster Nov 20 '11 at 03:18
  • What would you want back if hello and world were seperated by more than one space? – Tony Hopkinson Nov 20 '11 at 03:27

3 Answers3

16

You can use Regex.Split() for this. If you enclose the pattern in capturing parentheses, it will be included in the result too:

Regex.Split("Hello World", "( )")

gives you exactly what you wanted.

svick
  • 236,525
  • 50
  • 385
  • 514
1

You can use a regex, although it probably is an overkill :

StringCollection resultList = new StringCollection();
Regex regexObj = new Regex(@"(?:\b\w+\b|\s)");
Match matchResult = regexObj.Match(subjectString);
while (matchResult.Success) {
    resultList.Add(matchResult.Value);
    matchResult = matchResult.NextMatch();
} 
FailedDev
  • 26,680
  • 9
  • 53
  • 73
1

If you split on just the word-boundary, you'll get something very close to what you ask.

    string[] arr = Regex.Split("A quick brown fox.", "\\b");

arr[] = { "", "A", " ", "quick", " ", "brown", " ", "fox", "." }

phatfingers
  • 9,770
  • 3
  • 30
  • 44