24

提取字符串的整数部分的最佳方法是什么

Hello123

您如何获得 123 部分。你可以使用 Java 的 Scanner 来破解它,有没有更好的方法?

4

8 回答 8

43

如前所述,尝试使用正则表达式。这应该会有所帮助:

String value = "Hello123";
String intValue = value.replaceAll("[^0-9]", ""); // returns 123

然后你只需从那里将其转换为 int(或 Integer)。

于 2009-12-14T21:32:13.723 回答
26

我相信您可以执行以下操作:

Scanner in = new Scanner("Hello123").useDelimiter("[^0-9]+");
int integer = in.nextInt();

编辑:添加了 Carlos 的 useDelimiter 建议

于 2009-12-14T20:27:08.833 回答
11

为什么不直接使用正则表达式来匹配您想要的字符串部分?

[0-9]

这就是你所需要的,加上它需要的任何周围的字符。

查看http://www.regular-expressions.info/tutorial.html以了解正则表达式的工作原理。

编辑:我想说的是,如果其他提交者发布的代码确实有效,那么对于这个例子来说,Regex 可能有点过火......但我仍然建议总体上学习 Regex,因为它们非常强大,并且会比我想承认的更有用(在等了几年之后再试一试)。

于 2009-12-14T20:26:12.670 回答
4

假设您想要一个尾随数字,这将起作用:

import java.util.regex.*;

public class Example {


    public static void main(String[] args) {
        Pattern regex = Pattern.compile("\\D*(\\d*)");
        String input = "Hello123";
        Matcher matcher = regex.matcher(input);

        if (matcher.matches() && matcher.groupCount() == 1) {
            String digitStr = matcher.group(1);
            Integer digit = Integer.parseInt(digitStr);
            System.out.println(digit);            
        }

        System.out.println("done.");
    }
}
于 2009-12-14T21:45:23.593 回答
4

我一直认为 Michael 的正则表达式可能是最简单的解决方案,但再想一想,如果您使用 Matcher.find() 而不是 Matcher.matches(),则只有 "\d+" 有效:

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class Example {

    public static void main(String[] args) {
        String input = "Hello123";
        int output = extractInt(input);

        System.out.println("input [" + input + "], output [" + output + "]");
    }

    //
    // Parses first group of consecutive digits found into an int.
    //
    public static int extractInt(String str) {
        Matcher matcher = Pattern.compile("\\d+").matcher(str);

        if (!matcher.find())
            throw new NumberFormatException("For input string [" + str + "]");

        return Integer.parseInt(matcher.group());
    }
}
于 2009-12-15T04:57:48.000 回答
4

虽然我知道这是一个 6 年前的问题,但我正在为那些现在想避免学习正则表达式的人发布一个答案(顺便说一句,你应该这样做)。这种方法还给出了数字之间的数字(例如,HP 123 KT 567将返回 123567)

    Scanner scan = new Scanner(new InputStreamReader(System.in));
    System.out.print("Enter alphaNumeric: ");
    String x = scan.next();
    String numStr = "";
    int num;

    for (int i = 0; i < x.length(); i++) {
        char charCheck = x.charAt(i);
        if(Character.isDigit(charCheck)) {
            numStr += charCheck;
        }
    }

    num = Integer.parseInt(numStr);
    System.out.println("The extracted number is: " + num);
于 2016-08-27T16:25:20.323 回答
2

这对我来说非常有效。

    Pattern p = Pattern.compile("\\d+");
    Matcher m = p.matcher("string1234more567string890");
    while(m.find()) {
        System.out.println(m.group());
    }

输出:

1234
567
890
于 2019-10-27T07:33:25.760 回答
0
String[] parts = s.split("\\D+");    //s is string containing integers
int[] a;
a = new int[parts.length];
for(int i=0; i<parts.length; i++){
a[i]= Integer.parseInt(parts[i]);
System.out.println(a[i]);
} 
于 2018-10-17T19:28:12.840 回答