How to extract numbers from a string and get an array of ints?

后端 未结 13 821
孤城傲影
孤城傲影 2020-11-22 05:32

I have a String variable (basically an English sentence with an unspecified number of numbers) and I\'d like to extract all the numbers into an array of integers. I was wond

13条回答
  •  渐次进展
    2020-11-22 06:19

      StringBuffer sBuffer = new StringBuffer();
      Pattern p = Pattern.compile("[0-9]+.[0-9]*|[0-9]*.[0-9]+|[0-9]+");
      Matcher m = p.matcher(str);
      while (m.find()) {
        sBuffer.append(m.group());
      }
      return sBuffer.toString();
    

    This is for extracting numbers retaining the decimal

提交回复
热议问题