Reputation:
Is there any way to find the country code of a phone number in Java? Say if I give 9710334544, I will receive the country code as 91 (if its India).
Any suggestions please.
Upvotes: 0
Views: 4605
Reputation: 29553
If the phone number does not include the country code number as prefix, there is no way to find out from which region this phone number originates.
Upvotes: 5
Reputation: 8511
The idea behind the country code is to distinguish the country first, and then parse the number. The reason for this is to forego issues with the same number.
If my U.S. number is 1234567890
then what is there to distinguish that from my U.K number which is 1234567890
? The answer is the country prefix. Unfortunately, due to the very nature of this number(in that it distinguishes between numbers that are the same, you can't use the number to figure it out).
Now, if you already have the full 13-14 digit number, you can find the country code by simply dividing (integer division):
long inputPhoneNumber = 123 (XXX) - XXX - XXXX;
long countryCode = inputPhoneNumber / 10000000000l;
// Will give 123
After you have the answer you can match it up with the country, the internet provides several sites that list the codes with their countries:
Upvotes: 2