extracting a number from a string in Java

前端 未结 2 1058
时光取名叫无心
时光取名叫无心 2020-12-21 07:22

I have a string with a number inside and I want to retrieve that number. for example if I have a string \"bla bla 45 bla bla\" I want to get the number 45. I have searched a

相关标签:
2条回答
  • 2020-12-21 07:48

    You should use matcher.find() instead.

    0 讨论(0)
  • 2020-12-21 08:06

    Here's an example on how to use matcher.find()

        Matcher matcher = Pattern.compile("\\d+").matcher("bla bla 45 bla 22 bla");
        while(matcher.find()) {
            System.out.println(matcher.group());
        }
    

    This will output

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