Java:如何正则表达式或子字符串利用此字符串?

发布于 2024-11-17 07:01:02 字数 324 浏览 6 评论 0原文

"Current Peak :   830           300"

我怎样才能得到这两个数字,但没有空格。 (每个子字符串之间有 5 个或更多空格)

使用 apache 的 StringUtils 我到目前为止得到:

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

这给出了:

"830           300"

但仍然有很多空格并且没有分隔。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

酒与心事 2024-11-24 07:01:02

或者只使用核心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"

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"
简单气质女生网名 2024-11-24 07:01:02

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

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

Untested, but this should give you your two numbers:

String[] workUnits = StringUtils.split(StringUtils.substringAfter(thaString, ":"));
新一帅帅 2024-11-24 07:01:02

你可以这样做:

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
    // ...
}

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
    // ...
}
信仰 2024-11-24 07:01:02
String[] workUnits = StringUtils.substringAfter(thaString,":").split("\\s+");
String[] workUnits = StringUtils.substringAfter(thaString,":").split("\\s+");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文