SPLIT A STRING WITH ANY WHITESPACE CHARS AS DELIMITERS?

john rob

Total Post:108

Points:756
Posted by  john rob
Java  String  Split 
 708  View(s)
Ratings:
Rate this:
What regex pattern would need I to pass to the java.lang.String.split() method to split a String into an Array of substrings using all whitespace characters (' ', '\t', '\n', etc.) as delimiters?
  1. Mayank Tripathi

    Post:397

    Points:3117
    Re: Split a string with any whitespace chars as delimiters?

    Something in the lines of

    myString.split("\\s+");
    This groups all white spaces as a delimiter.

    So if I have the string:

    "Hello[space][tab]World"

    This should yield the strings "Hello" and "World" and omit the empty space between the [space] and the [tab].

    As VonC pointed out, the backslash should be escaped, because Java would first try to escape the string to a special character, and send that to be parsed. What you want, is the literal "\s", which means, you need to pass "\\s". It can get a bit confusing.

    The \\s is equivalent to [ \\t\\n\\x0B\\f\\r]

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!