【问题标题】:I want to separate several parts of a String in Java [closed]我想在Java中分离字符串的几个部分[关闭]
【发布时间】:2017-11-12 14:23:19
【问题描述】:

我想从字符串XmodX 中分离出两个整数值,一个在 mod 之前,另一个在 mod 之后,整数可以是任意长度。最好的方法是什么?

【问题讨论】:

标签: java regex


【解决方案1】:

这种方法应该可行...

// The variable 'parts' will contain 2 items:  your 2 integers, though they will still be String objects
String[] parts = myString.split("mod");

try {
    int firstInt = Integer.parseInt(parts[0]);
    int secondInt = Integer.parseInt(parts[1]);
) catch(NumberFormatException nfe) {
  // One of your Strings was not an integer value
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-06
    • 1970-01-01
    相关资源
    最近更新 更多