Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python split semantics in Java

When I split a string in python, adjacent space delimiters are merged:

>>> str = "hi              there"
>>> str.split()
['hi', 'there']

In Java, the delimiters are not merged:

$ cat Split.java
class Split {
    public static void main(String args[]) {
        String str = "hi              there";
        String result = "";
        for (String tok : str.split(" "))
            result += tok + ",";
        System.out.println(result);
    }
}
$ javac Split.java ; java Split
hi,,,,,,,,,,,,,,there,

Is there a straightforward way to get python space split semantics in java?

like image 832
Andrew Prock Avatar asked Dec 28 '22 02:12

Andrew Prock


2 Answers

String.split accepts a regular expression, so provide it with one that matches adjacent whitespace:

str.split("\\s+")

If you want to emulate the exact behaviour of Python's str.split(), you'd need to trim as well:

str.trim().split("\\s+")

Quote from the Python docs on str.split():

If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a None separator returns [].

So the above is still not an exact equivalent, because it will return [''] for the empty string, but it's probably okay for your purposes :)

like image 59
Niklas B. Avatar answered Jan 09 '23 12:01

Niklas B.


Use str.split("\\s+") instead. This will do what you need.

like image 33
Eugene Retunsky Avatar answered Jan 09 '23 11:01

Eugene Retunsky