How to Separate a Country Code from a Phone Number in Android

How do I separate a country code from a phone number in Android?

I've used ridsatrio's answer with an older question here

I'm getting the country code out of the string array below it with the following class:

import android.widget.TextView;

/**
* Created by Friso on 14/11/21.
*/
public final class PhoneFunctions {
private static PhoneFunctions instance;

private PhoneFunctions(){}

public static PhoneFunctions getInstance() {
if (instance == null) {
instance = new PhoneFunctions();
}

return instance;
}

public String getCountry(String[] argStringArray, TextView argText){
String country="";

if (argText.getText().toString().length() >= 4){
for(int i=0;i<argStringArray.length;i++){
String[] g=argStringArray[i].split(",");
if(g[0].equals(getFirstFourChar(argText))){
country=g[1];
break;
}
if (g[0].equals(getFirstThreeChar(argText))){
country=g[1];
break;
}
if (g[0].equals(getFirstTwoChar(argText))){
country=g[1];
break;
}
}
}

return country;
}

public String getFirstFourChar(TextView argText){
String threeChar;
String text = argText.getText().toString();
threeChar = text.substring(0,4);

return threeChar;
}

public String getFirstThreeChar(TextView argText){
String twoChar;
String text = argText.getText().toString();
twoChar = text.substring(0,3);

return twoChar;
}

public String getFirstTwoChar(TextView argText){
String oneChar;
String text = argText.getText().toString();
oneChar = text.substring(0,2);

return oneChar;
}
}

The country codes consist of 1-3 numbers and because this app had to have + string at the beginning of the phone number I thought it faster to just add a + to codes in the string array that I'm comparing to instead of adding more code to separate the +.

<string-array name="CountryCodes" >
<item>+93,AF</item>
<item>+355,AL</item>
<item>+213,DZ</item>
<item>+376,AD</item>
<item>+244,AO</item>
<item>+672,AQ</item>
<item>+54,AR</item>
<item>+374,AM</item>
<item>+297,AW</item>
<item>+61,AU</item>
<item>+43,AT</item>
<item>+994,AZ</item>
<item>+973,BH</item>
<item>+880,BD</item>
<item>+375,BY</item>
<item>+32,BE</item>
<item>+501,BZ</item>
<item>+229,BJ</item>
<item>+975,BT</item>
<item>+591,BO</item>
<item>+387,BA</item>
<item>+267,BW</item>
<item>+55,BR</item>
<item>+673,BN</item>
<item>+359,BG</item>
<item>+226,BF</item>
<item>+95,MM</item>
<item>+257,BI</item>
<item>+855,KH</item>
<item>+237,CM</item>
<item>+1,CA</item>
<item>+238,CV</item>
<item>+236,CF</item>
<item>+235,TD</item>
<item>+56,CL</item>
<item>+86,CN</item>
<item>+61,CX</item>
<item>+61,CC</item>
<item>+57,CO</item>
<item>+269,KM</item>
<item>+242,CG</item>
<item>+243,CD</item>
<item>+682,CK</item>
<item>+506,CR</item>
<item>+385,HR</item>
<item>+53,CU</item>
<item>+357,CY</item>
<item>+420,CZ</item>
<item>+45,DK</item>
<item>+253,DJ</item>
<item>+670,TL</item>
<item>+593,EC</item>
<item>+20,EG</item>
<item>+503,SV</item>
<item>+240,GQ</item>
<item>+291,ER</item>
<item>+372,EE</item>
<item>+251,ET</item>
<item>+500,FK</item>
<item>+298,FO</item>
<item>+679,FJ</item>
<item>+358,FI</item>
<item>+33,FR</item>
<item>+689,PF</item>
<item>+241,GA</item>
<item>+220,GM</item>
<item>+995,GE</item>
<item>+49,DE</item>
<item>+233,GH</item>
<item>+350,GI</item>
<item>+30,GR</item>
<item>+299,GL</item>
<item>+502,GT</item>
<item>+224,GN</item>
<item>+245,GW</item>
<item>+592,GY</item>
<item>+509,HT</item>
<item>+504,HN</item>
<item>+852,HK</item>
<item>+36,HU</item>
<item>+91,IN</item>
<item>+62,ID</item>
<item>+98,IR</item>
<item>+964,IQ</item>
<item>+353,IE</item>
<item>+44,IM</item>
<item>+972,IL</item>
<item>+39,IT</item>
<item>+225,CI</item>
<item>+81,JP</item>
<item>+962,JO</item>
<item>+7,KZ</item>
<item>+254,KE</item>
<item>+686,KI</item>
<item>+965,KW</item>
<item>+996,KG</item>
<item>+856,LA</item>
<item>+371,LV</item>
<item>+961,LB</item>
<item>+266,LS</item>
<item>+231,LR</item>
<item>+218,LY</item>
<item>+423,LI</item>
<item>+370,LT</item>
<item>+352,LU</item>
<item>+853,MO</item>
<item>+389,MK</item>
<item>+261,MG</item>
<item>+265,MW</item>
<item>+60,MY</item>
<item>+960,MV</item>
<item>+223,ML</item>
<item>+356,MT</item>
<item>+692,MH</item>
<item>+222,MR</item>
<item>+230,MU</item>
<item>+262,YT</item>
<item>+52,MX</item>
<item>+691,FM</item>
<item>+373,MD</item>
<item>+377,MC</item>
<item>+976,MN</item>
<item>+382,ME</item>
<item>+212,MA</item>
<item>+258,MZ</item>
<item>+264,NA</item>
<item>+674,NR</item>
<item>+977,NP</item>
<item>+31,NL</item>
<item>+599,AN</item>
<item>+687,NC</item>
<item>+64,NZ</item>
<item>+505,NI</item>
<item>+227,NE</item>
<item>+234,NG</item>
<item>+683,NU</item>
<item>+850,KP</item>
<item>+47,NO</item>
<item>+968,OM</item>
<item>+92,PK</item>
<item>+680,PW</item>
<item>+507,PA</item>
<item>+675,PG</item>
<item>+595,PY</item>
<item>+51,PE</item>
<item>+63,PH</item>
<item>+870,PN</item>
<item>+48,PL</item>
<item>+351,PT</item>
<item>+1,PR</item>
<item>+974,QA</item>
<item>+40,RO</item>
<item>+7,RU</item>
<item>+250,RW</item>
<item>+590,BL</item>
<item>+685,WS</item>
<item>+378,SM</item>
<item>+239,ST</item>
<item>+966,SA</item>
<item>+221,SN</item>
<item>+381,RS</item>
<item>+248,SC</item>
<item>+232,SL</item>
<item>+65,SG</item>
<item>+421,SK</item>
<item>+386,SI</item>
<item>+677,SB</item>
<item>+252,SO</item>
<item>+27,ZA</item>
<item>+82,KR</item>
<item>+34,ES</item>
<item>+94,LK</item>
<item>+290,SH</item>
<item>+508,PM</item>
<item>+249,SD</item>
<item>+597,SR</item>
<item>+268,SZ</item>
<item>+46,SE</item>
<item>+41,CH</item>
<item>+963,SY</item>
<item>+886,TW</item>
<item>+992,TJ</item>
<item>+255,TZ</item>
<item>+66,TH</item>
<item>+228,TG</item>
<item>+690,TK</item>
<item>+676,TO</item>
<item>+216,TN</item>
<item>+90,TR</item>
<item>+993,TM</item>
<item>+688,TV</item>
<item>+971,AE</item>
<item>+256,UG</item>
<item>+44,GB</item>
<item>+380,UA</item>
<item>+598,UY</item>
<item>+1,US</item>
<item>+998,UZ</item>
<item>+678,VU</item>
<item>+39,VA</item>
<item>+58,VE</item>
<item>+84,VN</item>
<item>+681,WF</item>
<item>+967,YE</item>
<item>+260,ZM</item>
<item>+263,ZW</item>
</string-array>

separating country code from mobile number if present android

Getting telephone country code with Android
This link worked for me.I am posting the link for all those looking for an answer like me

Extract code country from phone number [libphonenumber]

Okay, so I've joined the google group of libphonenumber ( https://groups.google.com/forum/?hl=en&fromgroups#!forum/libphonenumber-discuss ) and I've asked a question.

I don't need to set the country in parameter if my phone number begins with "+". Here is an example :

PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
try {
// phone must begin with '+'
PhoneNumber numberProto = phoneUtil.parse(phone, "");
int countryCode = numberProto.getCountryCode();
} catch (NumberParseException e) {
System.err.println("NumberParseException was thrown: " + e.toString());
}

How to split mobile number into country code, area code and local number?

It's not possible to parse phone numbers with a simple algorithm, you need to use data tables populated with each country's rules - because each country delimits their phone numbers differently.

The country code is fairly easy, just use the data from from the Country calling codes article in wikipedia and build a table of all the unique country codes. Each country has a unique prefix, so that's easy.

But then you need to look up the rules for every country you want to support and extract the area code(s) using the rules for each country.

Android - phone number formatting and removing the country/area code

You can use PhoneNumberUtils.compare to compare and check if they are same or not.It returns true if they are same ignoring country codes etc.

Example:

PhoneNumberUtils.compare(context, 1234567890, +911234567890);

returns true

extract the country code from mobile number

There is a list of all the phone numbers available for country codes on Wikipedia. It is here

http://en.wikipedia.org/wiki/List_of_country_calling_codes

I would create two hashmaps. One with all the four digit codes and one containing both the two and three digit codes.

Hashmap fourdigitcodes;

//check if first digit is 7
if(firstdigit==7)
//country definitely russia
else
if country code begins with 1.
Check the first four digits are in the hashmap with four digit codes and return the value
if no value is found then answer is certainly usa.
otherwise
String country="";
HashMap<String,String> ccode = new HashMap<String,String>();
ccode.put("353","Ireland");//List all country codes
ccode.put("49","Germany");
String value = ccode.get("49");//See if I have a value for the first 2 digits
if(value != null){
country = value; //If I found a country for the first two digits we are done as the first two digits of the country code consisting of two digits cannot be in a country code with 3
}else{
the country is certainly 3 digit. return the first three digits by entering the first 3 digits as the key of the hashmap countaining the first three digits.
}

In summary. If first digit is 7 Russian.
If it is 1 check in the hashmap of four digit codes for the first four numbers
if found a four digit code return the corresponding country.if not answer is USA
otherwise check first two digits in the hashmap containing the 2 or 3 digits. If found two then return the answer other it is certainly a three digit code and return the corresponding country from the hash.

It will be tedious to make the hashmap but I think an efficient solution.

How to remove country code , When Pick Phone Number from contacts

use this function hope it will help you out:

public String phoeNumberWithOutCountryCode(String phoneNumberWithCountryCode) {
Pattern complie = Pattern.compile(" ");
String[] phonenUmber = complie.split(phoneNumberWithCountryCode);
Log.e("number is", phonenUmber[1]);
return phonenUmber[1];
}


Related Topics



Leave a reply



Submit