Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split the String by number

Tags:

java

string

Can anyone help me to split the string by number Eg: "I Need 5000 points" is the string I want "5000" from that string.

I tried many ways like:

//split the string by RegExp:
String array = string.split(".*\\d.")

I am getting the output but its not what I expect

Output:

 array[0] = ""
 array[1]  ="points

Can anyone help me to find the proper solution?

like image 770
Satish Avatar asked May 25 '26 15:05

Satish


1 Answers

Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher("I Need 5000 points");
while (matcher.find()) {            
    System.out.println(matcher.group());
}
like image 110
Melih Altıntaş Avatar answered May 28 '26 05:05

Melih Altıntaş



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!