Can I split the number to country code and the actual number?

2,489 views
Skip to first unread message

Avi Hayun

unread,
Jun 21, 2012, 11:17:34 AM6/21/12
to libphonenum...@googlegroups.com
It might be a simple question, but I have an app which gets an international number.

I want to split it to country code and number.

How can I do it?
Is there a way of doing it?

Shaopeng

unread,
Jun 21, 2012, 11:33:20 AM6/21/12
to libphonenum...@googlegroups.com
Hello Avi,

What do the international numbers you get look like?

Basically you just need to parse the number to a PhoneNumber object. After that, you can get country code and number from the object using its getters.

Cheers,

Shaopeng

2012/6/21 Avi Hayun <avra...@gmail.com>

Avi Hayun

unread,
Jun 21, 2012, 11:39:22 AM6/21/12
to libphonenum...@googlegroups.com
This is an example of a phone number:
972527774444

Where 972 is Israel, 52 is the area code (mobile in this case) and 7774444is the actual number.

I tried using the PhoneNumberUtil to parse it, but it asks for a region code "IL" - in this example and I don't know which region code will be used...

That is where I got stuck.


If I knew that 972 is the prefix then i could use the following on it:
PhoneNumberUtil.getRegionCodeForCountryCode(prefix);


Can libPhonenumber library help me identify 972 as the region code?


On Thursday, 21 June 2012 18:33:20 UTC+3, Shaopeng wrote:
Hello Avi,

What do the international numbers you get look like?

Basically you just need to parse the number to a PhoneNumber object. After that, you can get country code and number from the object using its getters.

Cheers,

Shaopeng

2012/6/21 Avi Hayun
It might be a simple question, but I have an app which gets an international number.

Shaopeng

unread,
Jun 21, 2012, 11:43:09 AM6/21/12
to libphonenum...@googlegroups.com
You don't need the region code if all your phone numbers start with "+" followed by the country calling code; otherwise you will have to supply a default region code.

2012/6/21 Avi Hayun <avra...@gmail.com>

Avi Hayun

unread,
Jun 21, 2012, 12:12:02 PM6/21/12
to libphonenum...@googlegroups.com
Thank you very much.

It worked as you have suggested.

I have added the "+" sign and then parsed it successfully.


I will post the code for the benefits of others:

String    phoneNumber       = phoneNumberUtil.parse("+" + prefix + number, "ZZ");
boolean isValidated = phoneNumberUtil.isValidNumber(phoneNumber);
Log("myTag", "PhoneNumber: " + phoneNumber + "\nSuccessfull? " + isValidated);



Avi.

Lara Rennie

unread,
Jun 21, 2012, 12:39:30 PM6/21/12
to libphonenum...@googlegroups.com


2012/6/21 Shaopeng <jia.sh...@gmail.com>

Hello Avi,

What do the international numbers you get look like?

Basically you just need to parse the number to a PhoneNumber object. After that, you can get country code and number from the object using its getters.

Just to clarify: you should NOT get the number this way. You should use getNationalSignificantNumber instead. Country code you can get fine using the getters.

Avi Hayun

unread,
Jun 21, 2012, 1:01:09 PM6/21/12
to libphonenum...@googlegroups.com
Hi Lara,

I looked at your 1 line answer and tried to understand it for several minutes now.

Why can't I use the getter for the national number?
Because it is a long instead of a String?


And if the method you suggested is better then it should be implemented in the regular getter.



Anyway,I took your advice and use that method number for the national number.


Thanks,
Avi. 

Lara Rennie

unread,
Jun 21, 2012, 1:03:36 PM6/21/12
to libphonenum...@googlegroups.com


2012/6/21 Avi Hayun <avra...@gmail.com>

Hi Lara,

I looked at your 1 line answer and tried to understand it for several minutes now.

Why can't I use the getter for the national number?
Because it is a long instead of a String?

No, because of the italian leading zero field. Some numbers have a leading zero that is not the national prefix, and this needs to be added as a prefix when required.
 


And if the method you suggested is better then it should be implemented in the regular getter.

Yes, it will be one day when we have an Immutable Phone Number object. The reason it isn't is because the phone number object is based on proto generation, and we couldn't do custom methods like that.
Reply all
Reply to author
Forward
0 new messages