I need to get parts of a String
in Java. I know you can with a split
, but I can't get the parts I need.
This is my chain 57mm x 37mm
.
I need both numbers, 57 and 37
I 57
can already get it by:
String[] parts = tama.split("mm");
String to = parts[0];
Now I need the37
I suggest you use in this case as separator the character "x"
use .replace() to replace "mm" with empty String and .trim() to remove whitespace:
Another option is to use a regular expression like the one suggested by @SeanOwen :
for instance:
It can be done using a regular expression that retrieves the numbers present in the string, like so:
This will print an array with the numbers:
It uses
Pattern
that it receives a regular expression where you indicate that you expect numbers: