2
"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.

Stephan Kristyn
  • 15,015
  • 14
  • 88
  • 147

4 Answers4

6

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"
Bohemian
  • 412,405
  • 93
  • 575
  • 722
3

Untested, but this should give you your two numbers:

String[] workUnits = StringUtils.split(StringUtils.substringAfter(thaString, ":"));
Kevin
  • 3,771
  • 2
  • 31
  • 40
  • Perfect! System.out.println(workUnits[i]); gives back the resp. values. I liked your first solution (edited away) w/o apache's library, String [] temp = myArray[3].split(" "); however System.out.println(temp[temp.length-1]); gave only back the last number .. – Stephan Kristyn Jun 26 '11 at 23:48
1

You can do something like this:

Pattern patt = Pattern.compile("Current Peak :\\s*(\\d*)\\s*(\\d*)");
Matcher matcher = patt.matcher("Current Peak :   830           300");
if (matcher.find()) {
    String first  = matcher.group(1); // 830
    String second = matcher.group(2); // 300
    // ...
}
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
0
String[] workUnits = StringUtils.substringAfter(thaString,":").split("\\s+");
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147