Extract digits from string - StringUtils Java

前端 未结 18 1234
忘掉有多难
忘掉有多难 2020-12-01 09:06

I have a String and I want to extract the (only) sequence of digits in the string.

Example: helloThisIsA1234Sample. I want the 1234

It\'s a given that the s

18条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-01 09:17

    You can also use java.util.Scanner:

    new Scanner(str).useDelimiter("[^\\d]+").nextInt()

    You can use next() instead of nextInt() to get the digits as string.

    You can check for the presence of number using hasNextInt() on the Scanner.

提交回复
热议问题