Java: How to RegEx or Substring Utils this String?

stevek-pro picture stevek-pro · Jun 27, 2011 · Viewed 7.5k times · Source
"Current Peak :   830           300"

How can I get hold of the two numbers, but without the spaces. (There are 5 or more spaces in between each substring)

Using apache's StringUtils I got so far:

String workUnit =
StringUtils.substringAfter(thaString,
":");

This gives back:

"830           300"

but still with a lot of spaces and not separated.

Answer

Bohemian picture Bohemian · Jun 27, 2011

Or just using core java:

    String input = "Current Peak :   830           300";
    String[] parts = input.split("\\s+"); // Split on any number of whitespace
    String num1 = parts[3]; // "830"
    String num2 = parts[4]; // "300"