number out of string in java

后端 未结 6 1433
谎友^
谎友^ 2020-12-19 06:49

I have something like \"ali123hgj\". i want to have 123 in integer. how can i make it in java?

相关标签:
6条回答
  • 2020-12-19 07:14

    Use the following RegExp (see http://java.sun.com/docs/books/tutorial/essential/regex/):

    \d+
    

    By:

    final Pattern pattern = Pattern.compile("\\d+"); // the regex
    final Matcher matcher = pattern.matcher("ali123hgj"); // your string
    
    final ArrayList<Integer> ints = new ArrayList<Integer>(); // results
    
    while (matcher.find()) { // for each match
        ints.add(Integer.parseInt(matcher.group())); // convert to int
    }
    
    0 讨论(0)
  • 2020-12-19 07:21
    public static final List<Integer> scanIntegers2(final String source) {
        final ArrayList<Integer> result = new ArrayList<Integer>(); 
        // in real life define this as a static member of the class.
        // defining integers -123, 12 etc as matches.
        final Pattern integerPattern = Pattern.compile("(\\-?\\d+)");
        final Matcher matched = integerPattern.matcher(source);
        while (matched.find()) {
         result.add(Integer.valueOf(matched.group()));
        }
        return result;
    

    Input "asg123d ddhd-2222-33sds --- ---222 ss---33dd 234" results in this ouput [123, -2222, -33, -222, -33, 234]

    0 讨论(0)
  • 2020-12-19 07:24
    int index = -1;
    for (int i = 0; i < str.length(); i++) {
       if (Character.isDigit(str.charAt(i)) {
          index = i; // found a digit
          break;
       }
    }
    if (index >= 0) {
       int value = String.parseInt(str.substring(index)); // parseInt ignores anything after the number
    } else {
       // doesn't contain int...
    }
    
    0 讨论(0)
  • 2020-12-19 07:27

    This is the Google Guava #CharMatcher Way.

    String alphanumeric = "12ABC34def";
    
    String digits = CharMatcher.JAVA_DIGIT.retainFrom(alphanumeric); // 1234
    
    String letters = CharMatcher.JAVA_LETTER.retainFrom(alphanumeric); // ABCdef
    

    If you only care to match ASCII digits, use

    String digits = CharMatcher.inRange('0', '9').retainFrom(alphanumeric); // 1234
    

    If you only care to match letters of the Latin alphabet, use

    String letters = CharMatcher.inRange('a', 'z')
                             .or(inRange('A', 'Z')).retainFrom(alphanumeric); // ABCdef
    
    0 讨论(0)
  • 2020-12-19 07:33
    int i = Integer.parseInt("blah123yeah4yeah".replaceAll("\\D", ""));
    // i == 1234
    

    Note how this will "merge" digits from different parts of the strings together into one number. If you only have one number anyway, then this still works. If you only want the first number, then you can do something like this:

    int i = Integer.parseInt("x-42x100x".replaceAll("^\\D*?(-?\\d+).*$", "$1"));
    // i == -42
    

    The regex is a bit more complicated, but it basically replaces the whole string with the first sequence of digits that it contains (with optional minus sign), before using Integer.parseInt to parse into integer.

    0 讨论(0)
  • 2020-12-19 07:39

    You could probably do it along these lines:

    Pattern pattern = Pattern.compile("[^0-9]*([0-9]*)[^0-9]*");
    Matcher matcher = pattern.matcher("ali123hgj");
    boolean matchFound = matcher.find();
    if (matchFound) {
        System.out.println(Integer.parseInt(matcher.group(0)));
    }
    

    It's easily adaptable to multiple number group as well. The code is just for orientation: it hasn't been tested.

    0 讨论(0)
提交回复
热议问题