How to extract all numbers from a string in java?

How to extract all numbers from a string in java?

Suppose the String is n
then array s will contain all the numbers present in the String

String n=sc.next();

String[] s = n.split("[a-zA-Z]+");

eg
n=“abc1defghi23jvkfbd57jnfbddj9”

then s will be

1,23,57,9

2 Likes

String str=“sdfvsdf68fsdfsf8999fsdf09”;
String numberOnly= str.replaceAll("[^0-9]", “”);

anything other then 0-9 will be replaced even the , ? ! and all other things .

//