0

So I want to be able do split this string by spaces: "1 ½ cups fat-free half-and-half, divided "

I wrote my code like this:

String trimmed;
String[] words = trimmed.split(" ");

But it doesn't work! The 1 and the ½ end up in the same position of the array.

I also tried How to split a string with any whitespace chars as delimiters but it does not split string either. Looking in text editor there is clearly some sort of "space" but I don't get how to split on it. Is is because of "½"?

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179

2 Answers2

1

You've got a thin space there instead of a "regular" space character.

enter image description here

Regex capturing of this is not trivial, as there are other character classes you need to capture. You would at a minimum want to capture it as an additional grouping...

System.out.println(Arrays.toString(s.split("(\\s|\\u2009)")));

...but you would also need to include all the other non-standard white space characters in this search just to be sure you don't miss any. The above works for your case.

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • You don't need the parentheses around the pattern, and you can directly use the Unicode escape in the String literal, no need to escape it. – Marcono1234 Feb 24 '21 at 23:26
1

The reason for this is that the space between 1 and ½ is not a regular space (U+0020) but instead a "thin space" (U+2009).

Since String.split(String) accepts a regex pattern, you could for example use the pattern \h instead which represents a "horizontal whitespace character", see Pattern documentation, and matches U+2009.
Or you could use the pattern " |\u2009".

Marcono1234
  • 5,856
  • 1
  • 25
  • 43