简体   繁体   中英

Java: How to RegEx or Substring Utils this String?

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

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"

未经测试,但这应该给你两个数字:

String[] workUnits = StringUtils.split(StringUtils.substringAfter(thaString, ":"));

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
    // ...
}
String[] workUnits = StringUtils.substringAfter(thaString,":").split("\\s+");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM