private static String countryCodeForPhoneNumber(String normalizedPhoneNumber){
  final String phoneWithoutPlusPrefix=normalizedPhoneNumber.replaceFirst("^\\+","");
  final int numberLength=phoneWithoutPlusPrefix.length();
  for (int i=1; i <= MAX_LENGTH_COUNTRY_CODE && i <= numberLength; i++) {
    final String potentialCountryCode=phoneWithoutPlusPrefix.substring(0,i);
    final Integer countryCodeKey=Integer.valueOf(potentialCountryCode);
    if (CountryCodeToRegionCodeMap.containsKey(countryCodeKey)) {
      return potentialCountryCode;
    }
  }
  return DEFAULT_COUNTRY_CODE;
}

private static String countryCodeForPhoneNumber ( String normalizedPhoneNumber ) { final String phoneWithoutPlusPrefix = normalizedPhoneNumber . replaceFirst ( "^\\+" , "" ) ; final int numberLength = phoneWithoutPlusPrefix . length ( ) ; for ( int i = 1 ; i <= MAX_LENGTH_COUNTRY_CODE && i <= numberLength ; i ++ ) { final String potentialCountryCode = phoneWithoutPlusPrefix . substring ( 0 , i ) ; final Integer countryCodeKey = Integer . valueOf ( potentialCountryCode ) ; if ( CountryCodeToRegionCodeMap . containsKey ( countryCodeKey ) ) { return potentialCountryCode ; } } return DEFAULT_COUNTRY_CODE ; }
country code extracted using shortest matching prefix like libphonenumber . see : https : / / github . com / googlei18n / libphonenumber / blob / master / java / libphonenumber / src / com / google / i18n / phonenumbers / phonenumberutil . java # l2395
