I have a string
String exp = "7 to 10";
now I am keeping a condition that
if (exp.contains("to"))
{
// here I want to fetch the integers 7 and 10
}
How to separate 7 and 10 from the string 7 to 10 (parsed as Integer).
By using a delimiter I can obviously do it but I want to know how to do it this way?
Using split:
if (exp.contains(" to ")) {
String[] numbers = exp.split(" to ");
// convert string to numbers
}
Using regex:
Matcher mat = Pattern.compile("(\\d+) to (\\d+)").matcher(exp);
if (mat.find()) {
String first = mat.group(1);
String second = mat.group(2);
// convert string to numbers
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With