Hi everyone I am trying to get the numeric value inside a string. So far this method works well for me to obtain integers. But now I would also like to be able to obtain numbers that contain decimals.
if I want to extract the number that a String contains I use: getNumbers("delay: 5 days") output = 5
Now I want to get the number of "this a sample, delay: 7.1 days" output = 7.1 Remark: the str always can change, but always will have a number (integer or float)
public String getNumbers(String str){
str = str.replaceAll("[^\\d]", " ");
str = str.trim();
str = str.replaceAll(" +", " ");
if (str.equals(""))
return "-1";
return str;
}
Double#parseDouble(String)accomplish your needs?replaceAll,trimandreplaceAllseems a little cumbersome...