number out of string in java

后端 未结 6 1431
谎友^
谎友^ 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 ints = new ArrayList(); // results
    
    while (matcher.find()) { // for each match
        ints.add(Integer.parseInt(matcher.group())); // convert to int
    }
    

提交回复
热议问题