number out of string in java

后端 未结 6 1451
谎友^
谎友^ 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:21

    public static final List scanIntegers2(final String source) {
        final ArrayList result = new ArrayList(); 
        // 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]

提交回复
热议问题