I'm getting a phone number from an external API call and I'm trying to detect if it is indeed a number (I don't care at this point if it's a valid phone number). For simplicity's sake, I tried:
String number = phone.replace("+", "").replace(" ", "").trim();
if (number.matches("[0-9]+")) {
return true;
}
The string I'm getting in number
is something like 331234212453 but matches
returns false. What is weird is that when I inspect the number
variable, copy its value and try:
"331234212453".matches("[0-9]+")
It still does not work. Now if I type those same numbers and use matches
then I get it to work. Could there be some kind of encoding messing up with the string? And if so how do I fix it?
Edit: Forgot to say that I tried number.matches("\\d+")
and it also does not work.