how to extract numeric values from input string in java

后端 未结 15 1735
说谎
说谎 2020-12-13 16:25

How can I extract only the numeric values from the input string?

For example, the input string may be like this:

String str=\"abc d 1234567890pqr 548         


        
相关标签:
15条回答
  • 2020-12-13 16:56

    Split your string into char array using yourString.toCharArray(); Then iterate through the characters and use Character.isDigit(ch); to identify if this is the numeric value. Or iterate through whole string and use str.charAt(i). For e.g:

    public static void main(String[] args) {
        String str = "abc d 1234567890pqr 54897";
        StringBuilder myNumbers = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            if (Character.isDigit(str.charAt(i))) {
                myNumbers.append(str.charAt(i));
                System.out.println(str.charAt(i) + " is a digit.");
            } else {
                System.out.println(str.charAt(i) + " not a digit.");
            }
        }
        System.out.println("Your numbers: " + myNumbers.toString());
    }
    
    0 讨论(0)
  • 2020-12-13 16:56

    You could split the string on spaces to get the individual entries, loop across them, and try to parse them with the relevant method on Integer, using a try/catch approach to handle the cases where parsing it is as a number fails. That is probably the most straight-forward approach.

    Alternatively, you can construct a regex to match only the numbers and use that to find them all. This is probably far more performant for a big string. The regex will look something like `\b\d+\b'.

    UPDATE: Or, if this isn't homework or similar (I sort of assumed you were looking for clues to implementing it yourself, but that might not have been valid), you could use the solution that @npinti gives. That's probably the approach you should take in production code.

    0 讨论(0)
  • 2020-12-13 16:58

    If you do not want to use regex,

    String str = " abc d 1234567890pqr 54897";
    
    char[] chars = new char[str.length()];
    
    int i = 0;
    for (int j = 0; j < str.length(); j++) {
        char c = str.charAt(j);
        if (Character.isDigit(c)) {
            chars[i++] = c;
            if (j != chars.length - 1)
                continue;
        }
        if (chars[0] == '\0')
            continue;
        String num = new String(chars).trim();
        System.out.println(num);
        chars = new char[str.length()];
        i = 0;
    
    }
    

    Output : 1234567890 54897

    0 讨论(0)
  • 2020-12-13 16:58
            String line = "This order was32354 placed 343434for 43411 QT ! OK?";
            String regex = "[^\\d]+";
    
            String[] str = line.split(regex);
            String required = "";
            for(String st: str){
                System.out.println(st);
            }
    

    By above code you will get all the numeric values. then you can merge them or what ever you wanted to do with those numeric values.

    0 讨论(0)
  • 2020-12-13 17:04
    String str=" abc d 1234567890pqr 54897";
    Pattern pattern = Pattern.compile("\\w+([0-9]+)\\w+([0-9]+)");
    Matcher matcher = pattern.matcher(str);
    for(int i = 0 ; i < matcher.groupCount(); i++) {
      matcher.find();
      System.out.println(matcher.group());
    }
    
    0 讨论(0)
  • 2020-12-13 17:04

    You could use the .nextInt() method from the Scanner class:

    Scans the next token of the input as an int.

    Alternatively, you could also do something like so:

    String str=" abc d 1234567890pqr 54897";
    
    Pattern p = Pattern.compile("(\\d+)");
    Matcher m = p.matcher(str);
    while(m.find())
    {
        System.out.println(m.group(1));
    }
    
    0 讨论(0)
提交回复
热议问题