public class PhoneNumberUtil
extends java.lang.Object
If you use this library, and want to be notified about important changes, please sign up to our mailing list. NOTE: A lot of methods in this class require Region Code strings. These must be provided using CLDR two-letter region-code format. These should be in upper-case. The list of the codes can be found here: http://www.unicode.org/cldr/charts/30/supplemental/territory_information.html
Modifier and Type | Class and Description |
---|---|
static class |
PhoneNumberUtil.Leniency
Leniency when finding potential phone numbers in text
segments.
|
static class |
PhoneNumberUtil.MatchType
Types of phone number matches.
|
static class |
PhoneNumberUtil.PhoneNumberFormat
INTERNATIONAL and NATIONAL formats are consistent with the definition in ITU-T Recommendation
E.123.
|
static class |
PhoneNumberUtil.PhoneNumberType
Type of phone numbers.
|
static class |
PhoneNumberUtil.ValidationResult
Possible outcomes when testing if a PhoneNumber is possible.
|
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
REGION_CODE_FOR_NON_GEO_ENTITY |
Modifier and Type | Method and Description |
---|---|
boolean |
canBeInternationallyDialled(Phonenumber.PhoneNumber number)
Returns true if the number can be dialled from outside the region, or unknown.
|
static java.lang.String |
convertAlphaCharactersInNumber(java.lang.CharSequence number)
Converts all alpha characters in a number to their respective digits on a keypad, but retains
existing formatting.
|
static PhoneNumberUtil |
createInstance(MetadataLoader metadataLoader)
Create a new
PhoneNumberUtil instance to carry out international phone number
formatting, parsing, or validation. |
java.lang.Iterable<PhoneNumberMatch> |
findNumbers(java.lang.CharSequence text,
java.lang.String defaultRegion)
Returns an iterable over all
PhoneNumberMatches in text . |
java.lang.Iterable<PhoneNumberMatch> |
findNumbers(java.lang.CharSequence text,
java.lang.String defaultRegion,
PhoneNumberUtil.Leniency leniency,
long maxTries)
Returns an iterable over all
PhoneNumberMatches in text . |
java.lang.String |
format(Phonenumber.PhoneNumber number,
PhoneNumberUtil.PhoneNumberFormat numberFormat)
Formats a phone number in the specified format using default rules.
|
void |
format(Phonenumber.PhoneNumber number,
PhoneNumberUtil.PhoneNumberFormat numberFormat,
java.lang.StringBuilder formattedNumber)
Same as
#format(PhoneNumber, PhoneNumberFormat) , but accepts a mutable StringBuilder as
a parameter to decrease object creation when invoked many times. |
java.lang.String |
formatByPattern(Phonenumber.PhoneNumber number,
PhoneNumberUtil.PhoneNumberFormat numberFormat,
java.util.List<Phonemetadata.NumberFormat> userDefinedFormats)
Formats a phone number in the specified format using client-defined formatting rules.
|
java.lang.String |
formatInOriginalFormat(Phonenumber.PhoneNumber number,
java.lang.String regionCallingFrom)
Formats a phone number using the original phone number format that the number is parsed from.
|
java.lang.String |
formatNationalNumberWithCarrierCode(Phonenumber.PhoneNumber number,
java.lang.CharSequence carrierCode)
Formats a phone number in national format for dialing using the carrier as specified in the
carrierCode . |
java.lang.String |
formatNationalNumberWithPreferredCarrierCode(Phonenumber.PhoneNumber number,
java.lang.CharSequence fallbackCarrierCode)
Formats a phone number in national format for dialing using the carrier as specified in the
preferredDomesticCarrierCode field of the PhoneNumber object passed in.
|
java.lang.String |
formatNumberForMobileDialing(Phonenumber.PhoneNumber number,
java.lang.String regionCallingFrom,
boolean withFormatting)
Returns a number formatted in such a way that it can be dialed from a mobile phone in a
specific region.
|
java.lang.String |
formatOutOfCountryCallingNumber(Phonenumber.PhoneNumber number,
java.lang.String regionCallingFrom)
Formats a phone number for out-of-country dialing purposes.
|
java.lang.String |
formatOutOfCountryKeepingAlphaChars(Phonenumber.PhoneNumber number,
java.lang.String regionCallingFrom)
Formats a phone number for out-of-country dialing purposes.
|
AsYouTypeFormatter |
getAsYouTypeFormatter(java.lang.String regionCode)
Gets an
AsYouTypeFormatter for the specific region. |
int |
getCountryCodeForRegion(java.lang.String regionCode)
Returns the country calling code for a specific region.
|
static java.lang.String |
getCountryMobileToken(int countryCallingCode)
Returns the mobile token for the provided country calling code if it has one, otherwise
returns an empty string.
|
Phonenumber.PhoneNumber |
getExampleNumber(java.lang.String regionCode)
Gets a valid number for the specified region.
|
Phonenumber.PhoneNumber |
getExampleNumberForNonGeoEntity(int countryCallingCode)
Gets a valid number for the specified country calling code for a non-geographical entity.
|
Phonenumber.PhoneNumber |
getExampleNumberForType(PhoneNumberUtil.PhoneNumberType type)
Gets a valid number for the specified number type (it may belong to any country).
|
Phonenumber.PhoneNumber |
getExampleNumberForType(java.lang.String regionCode,
PhoneNumberUtil.PhoneNumberType type)
Gets a valid number for the specified region and number type.
|
static PhoneNumberUtil |
getInstance()
Gets a
PhoneNumberUtil instance to carry out international phone number formatting,
parsing, or validation. |
Phonenumber.PhoneNumber |
getInvalidExampleNumber(java.lang.String regionCode)
Gets an invalid number for the specified region.
|
int |
getLengthOfGeographicalAreaCode(Phonenumber.PhoneNumber number)
Gets the length of the geographical area code from the
PhoneNumber object passed in, so that clients could use it
to split a national significant number into geographical area code and subscriber number.
|
int |
getLengthOfNationalDestinationCode(Phonenumber.PhoneNumber number)
Gets the length of the national destination code (NDC) from the
PhoneNumber object passed in, so that clients could use it
to split a national significant number into NDC and subscriber number.
|
java.lang.String |
getNationalSignificantNumber(Phonenumber.PhoneNumber number)
Gets the national significant number of a phone number.
|
java.lang.String |
getNddPrefixForRegion(java.lang.String regionCode,
boolean stripNonDigits)
Returns the national dialling prefix for a specific region.
|
PhoneNumberUtil.PhoneNumberType |
getNumberType(Phonenumber.PhoneNumber number)
Gets the type of a valid phone number.
|
java.lang.String |
getRegionCodeForCountryCode(int countryCallingCode)
Returns the region code that matches the specific country calling code.
|
java.lang.String |
getRegionCodeForNumber(Phonenumber.PhoneNumber number)
Returns the region where a phone number is from.
|
java.util.List<java.lang.String> |
getRegionCodesForCountryCode(int countryCallingCode)
Returns a list with the region codes that match the specific country calling code.
|
java.util.Set<java.lang.Integer> |
getSupportedCallingCodes()
Returns all country calling codes the library has metadata for, covering both non-geographical
entities (global network calling codes) and those used for geographical entities.
|
java.util.Set<java.lang.Integer> |
getSupportedGlobalNetworkCallingCodes()
Returns all global network calling codes the library has metadata for.
|
java.util.Set<java.lang.String> |
getSupportedRegions()
Returns all regions the library has metadata for.
|
java.util.Set<PhoneNumberUtil.PhoneNumberType> |
getSupportedTypesForNonGeoEntity(int countryCallingCode)
Returns the types for a country-code belonging to a non-geographical entity which the library
has metadata for.
|
java.util.Set<PhoneNumberUtil.PhoneNumberType> |
getSupportedTypesForRegion(java.lang.String regionCode)
Returns the types for a given region which the library has metadata for.
|
boolean |
isAlphaNumber(java.lang.CharSequence number)
Checks if the number is a valid vanity (alpha) number such as 800 MICROSOFT.
|
boolean |
isMobileNumberPortableRegion(java.lang.String regionCode)
Returns true if the supplied region supports mobile number portability.
|
boolean |
isNANPACountry(java.lang.String regionCode)
Checks if this is a region under the North American Numbering Plan Administration (NANPA).
|
boolean |
isNumberGeographical(Phonenumber.PhoneNumber phoneNumber)
Tests whether a phone number has a geographical association.
|
boolean |
isNumberGeographical(PhoneNumberUtil.PhoneNumberType phoneNumberType,
int countryCallingCode)
Overload of isNumberGeographical(PhoneNumber), since calculating the phone number type is
expensive; if we have already done this, we don't want to do it again.
|
PhoneNumberUtil.MatchType |
isNumberMatch(java.lang.CharSequence firstNumber,
java.lang.CharSequence secondNumber)
Takes two phone numbers as strings and compares them for equality.
|
PhoneNumberUtil.MatchType |
isNumberMatch(Phonenumber.PhoneNumber firstNumber,
java.lang.CharSequence secondNumber)
Takes two phone numbers and compares them for equality.
|
PhoneNumberUtil.MatchType |
isNumberMatch(Phonenumber.PhoneNumber firstNumberIn,
Phonenumber.PhoneNumber secondNumberIn)
Takes two phone numbers and compares them for equality.
|
boolean |
isPossibleNumber(java.lang.CharSequence number,
java.lang.String regionDialingFrom)
Check whether a phone number is a possible number given a number in the form of a string, and
the region where the number could be dialed from.
|
boolean |
isPossibleNumber(Phonenumber.PhoneNumber number)
Convenience wrapper around
isPossibleNumberWithReason(com.google.i18n.phonenumbers.Phonenumber.PhoneNumber) . |
boolean |
isPossibleNumberForType(Phonenumber.PhoneNumber number,
PhoneNumberUtil.PhoneNumberType type)
|
PhoneNumberUtil.ValidationResult |
isPossibleNumberForTypeWithReason(Phonenumber.PhoneNumber number,
PhoneNumberUtil.PhoneNumberType type)
Check whether a phone number is a possible number of a particular type.
|
PhoneNumberUtil.ValidationResult |
isPossibleNumberWithReason(Phonenumber.PhoneNumber number)
Check whether a phone number is a possible number.
|
boolean |
isValidNumber(Phonenumber.PhoneNumber number)
Tests whether a phone number matches a valid pattern.
|
boolean |
isValidNumberForRegion(Phonenumber.PhoneNumber number,
java.lang.String regionCode)
Tests whether a phone number is valid for a certain region.
|
static java.lang.String |
normalizeDiallableCharsOnly(java.lang.CharSequence number)
Normalizes a string of characters representing a phone number.
|
static java.lang.String |
normalizeDigitsOnly(java.lang.CharSequence number)
Normalizes a string of characters representing a phone number.
|
Phonenumber.PhoneNumber |
parse(java.lang.CharSequence numberToParse,
java.lang.String defaultRegion)
Parses a string and returns it as a phone number in proto buffer format.
|
void |
parse(java.lang.CharSequence numberToParse,
java.lang.String defaultRegion,
Phonenumber.PhoneNumber phoneNumber)
Same as
parse(CharSequence, String) , but accepts mutable PhoneNumber as a
parameter to decrease object creation when invoked many times. |
Phonenumber.PhoneNumber |
parseAndKeepRawInput(java.lang.CharSequence numberToParse,
java.lang.String defaultRegion)
Parses a string and returns it in proto buffer format.
|
void |
parseAndKeepRawInput(java.lang.CharSequence numberToParse,
java.lang.String defaultRegion,
Phonenumber.PhoneNumber phoneNumber)
Same as
parseAndKeepRawInput(CharSequence, String) , but accepts a mutable
PhoneNumber as a parameter to decrease object creation when invoked many times. |
boolean |
truncateTooLongNumber(Phonenumber.PhoneNumber number)
Attempts to extract a valid number from a phone number that is too long to be valid, and resets
the PhoneNumber object passed in to that valid version.
|
public static final java.lang.String REGION_CODE_FOR_NON_GEO_ENTITY
public static java.lang.String normalizeDigitsOnly(java.lang.CharSequence number)
number
- a string of characters representing a phone numberpublic static java.lang.String normalizeDiallableCharsOnly(java.lang.CharSequence number)
number
- a string of characters representing a phone numberpublic static java.lang.String convertAlphaCharactersInNumber(java.lang.CharSequence number)
public int getLengthOfGeographicalAreaCode(Phonenumber.PhoneNumber number)
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
PhoneNumber number = phoneUtil.parse("16502530000", "US");
String nationalSignificantNumber = phoneUtil.getNationalSignificantNumber(number);
String areaCode;
String subscriberNumber;
int areaCodeLength = phoneUtil.getLengthOfGeographicalAreaCode(number);
if (areaCodeLength > 0) {
areaCode = nationalSignificantNumber.substring(0, areaCodeLength);
subscriberNumber = nationalSignificantNumber.substring(areaCodeLength);
} else {
areaCode = "";
subscriberNumber = nationalSignificantNumber;
}
N.B.: area code is a very ambiguous concept, so the I18N team generally recommends against
using it for most purposes, but recommends using the more general national_number
instead. Read the following carefully before deciding to use this method:
number
- the PhoneNumber object for which clients
want to know the length of the area codepublic int getLengthOfNationalDestinationCode(Phonenumber.PhoneNumber number)
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
PhoneNumber number = phoneUtil.parse("18002530000", "US");
String nationalSignificantNumber = phoneUtil.getNationalSignificantNumber(number);
String nationalDestinationCode;
String subscriberNumber;
int nationalDestinationCodeLength = phoneUtil.getLengthOfNationalDestinationCode(number);
if (nationalDestinationCodeLength > 0) {
nationalDestinationCode = nationalSignificantNumber.substring(0,
nationalDestinationCodeLength);
subscriberNumber = nationalSignificantNumber.substring(nationalDestinationCodeLength);
} else {
nationalDestinationCode = "";
subscriberNumber = nationalSignificantNumber;
}
Refer to the unittests to see the difference between this function and
getLengthOfGeographicalAreaCode(com.google.i18n.phonenumbers.Phonenumber.PhoneNumber)
.number
- the PhoneNumber object for which clients
want to know the length of the NDCpublic static java.lang.String getCountryMobileToken(int countryCallingCode)
countryCallingCode
- the country calling code for which we want the mobile tokenpublic java.util.Set<java.lang.String> getSupportedRegions()
public java.util.Set<java.lang.Integer> getSupportedGlobalNetworkCallingCodes()
public java.util.Set<java.lang.Integer> getSupportedCallingCodes()
public java.util.Set<PhoneNumberUtil.PhoneNumberType> getSupportedTypesForRegion(java.lang.String regionCode)
public java.util.Set<PhoneNumberUtil.PhoneNumberType> getSupportedTypesForNonGeoEntity(int countryCallingCode)
public static PhoneNumberUtil getInstance()
PhoneNumberUtil
instance to carry out international phone number formatting,
parsing, or validation. The instance is loaded with all phone number metadata.
The PhoneNumberUtil
is implemented as a singleton. Therefore, calling getInstance
multiple times will only result in one instance being created.
public static PhoneNumberUtil createInstance(MetadataLoader metadataLoader)
PhoneNumberUtil
instance to carry out international phone number
formatting, parsing, or validation. The instance is loaded with all metadata by
using the metadataLoader specified.
This method should only be used in the rare case in which you want to manage your own
metadata loading. Calling this method multiple times is very expensive, as each time
a new instance is created from scratch. When in doubt, use getInstance()
.
metadataLoader
- customized metadata loader. This should not be nullpublic boolean isNumberGeographical(Phonenumber.PhoneNumber phoneNumber)
public boolean isNumberGeographical(PhoneNumberUtil.PhoneNumberType phoneNumberType, int countryCallingCode)
public java.lang.String format(Phonenumber.PhoneNumber number, PhoneNumberUtil.PhoneNumberFormat numberFormat)
number
- the phone number to be formattednumberFormat
- the format the phone number should be formatted intopublic void format(Phonenumber.PhoneNumber number, PhoneNumberUtil.PhoneNumberFormat numberFormat, java.lang.StringBuilder formattedNumber)
#format(PhoneNumber, PhoneNumberFormat)
, but accepts a mutable StringBuilder as
a parameter to decrease object creation when invoked many times.public java.lang.String formatByPattern(Phonenumber.PhoneNumber number, PhoneNumberUtil.PhoneNumberFormat numberFormat, java.util.List<Phonemetadata.NumberFormat> userDefinedFormats)
number
- the phone number to be formattednumberFormat
- the format the phone number should be formatted intouserDefinedFormats
- formatting rules specified by clientspublic java.lang.String formatNationalNumberWithCarrierCode(Phonenumber.PhoneNumber number, java.lang.CharSequence carrierCode)
carrierCode
. The carrierCode
will always be used regardless of whether the
phone number already has a preferred domestic carrier code stored. If carrierCode
contains an empty string, returns the number in national format without any carrier code.number
- the phone number to be formattedcarrierCode
- the carrier selection code to be usedcarrierCode
public java.lang.String formatNationalNumberWithPreferredCarrierCode(Phonenumber.PhoneNumber number, java.lang.CharSequence fallbackCarrierCode)
fallbackCarrierCode
passed in instead. If there is no
preferredDomesticCarrierCode
, and the fallbackCarrierCode
contains an empty
string, return the number in national format without any carrier code.
Use formatNationalNumberWithCarrierCode(com.google.i18n.phonenumbers.Phonenumber.PhoneNumber, java.lang.CharSequence)
instead if the carrier code passed in
should take precedence over the number's preferredDomesticCarrierCode
when formatting.
number
- the phone number to be formattedfallbackCarrierCode
- the carrier selection code to be used, if none is found in the
phone number itselfpreferredDomesticCarrierCode
, or the fallbackCarrierCode
passed in if
none is foundpublic java.lang.String formatNumberForMobileDialing(Phonenumber.PhoneNumber number, java.lang.String regionCallingFrom, boolean withFormatting)
number
- the phone number to be formattedregionCallingFrom
- the region where the call is being placedwithFormatting
- whether the number should be returned with formatting symbols, such as
spaces and dashes.public java.lang.String formatOutOfCountryCallingNumber(Phonenumber.PhoneNumber number, java.lang.String regionCallingFrom)
If the number itself has a country calling code of zero or an otherwise invalid country calling code, then we return the number with no formatting applied.
Note this function takes care of the case for calling inside of NANPA and between Russia and Kazakhstan (who share the same country calling code). In those cases, no international prefix is used. For regions which have multiple international prefixes, the number in its INTERNATIONAL format will be returned instead.
number
- the phone number to be formattedregionCallingFrom
- the region where the call is being placedpublic java.lang.String formatInOriginalFormat(Phonenumber.PhoneNumber number, java.lang.String regionCallingFrom)
number
- the phone number that needs to be formatted in its original number formatregionCallingFrom
- the region whose IDD needs to be prefixed if the original number
has onepublic java.lang.String formatOutOfCountryKeepingAlphaChars(Phonenumber.PhoneNumber number, java.lang.String regionCallingFrom)
Caveats:
number
- the phone number that needs to be formattedregionCallingFrom
- the region where the call is being placedpublic java.lang.String getNationalSignificantNumber(Phonenumber.PhoneNumber number)
number
- the phone number for which the national significant number is neededpublic Phonenumber.PhoneNumber getExampleNumber(java.lang.String regionCode)
regionCode
- the region for which an example number is neededgetExampleNumberForNonGeoEntity(int)
instead.public Phonenumber.PhoneNumber getInvalidExampleNumber(java.lang.String regionCode)
ShortNumberInfo
.regionCode
- the region for which an example number is neededpublic Phonenumber.PhoneNumber getExampleNumberForType(java.lang.String regionCode, PhoneNumberUtil.PhoneNumberType type)
regionCode
- the region for which an example number is neededtype
- the type of number that is neededgetExampleNumberForNonGeoEntity(int)
instead.public Phonenumber.PhoneNumber getExampleNumberForType(PhoneNumberUtil.PhoneNumberType type)
type
- the type of number that is neededpublic Phonenumber.PhoneNumber getExampleNumberForNonGeoEntity(int countryCallingCode)
countryCallingCode
- the country calling code for a non-geographical entitypublic PhoneNumberUtil.PhoneNumberType getNumberType(Phonenumber.PhoneNumber number)
number
- the phone number that we want to know the typepublic boolean isValidNumber(Phonenumber.PhoneNumber number)
number
- the phone number that we want to validatepublic boolean isValidNumberForRegion(Phonenumber.PhoneNumber number, java.lang.String regionCode)
isValidNumber(com.google.i18n.phonenumbers.Phonenumber.PhoneNumber)
instead. For example, this
method will mark numbers from British Crown dependencies such as the Isle of Man as invalid for
the region "GB" (United Kingdom), since it has its own region code, "IM", which may be
undesirable.number
- the phone number that we want to validateregionCode
- the region that we want to validate the phone number forpublic java.lang.String getRegionCodeForNumber(Phonenumber.PhoneNumber number)
number
- the phone number whose origin we want to knowpublic java.lang.String getRegionCodeForCountryCode(int countryCallingCode)
public java.util.List<java.lang.String> getRegionCodesForCountryCode(int countryCallingCode)
public int getCountryCodeForRegion(java.lang.String regionCode)
regionCode
- the region that we want to get the country calling code forpublic java.lang.String getNddPrefixForRegion(java.lang.String regionCode, boolean stripNonDigits)
Warning: Do not use this method for do-your-own formatting - for some regions, the national dialling prefix is used only for certain types of numbers. Use the library's formatting functions to prefix the national prefix when required.
regionCode
- the region that we want to get the dialling prefix forstripNonDigits
- true to strip non-digits from the national dialling prefixpublic boolean isNANPACountry(java.lang.String regionCode)
public boolean isAlphaNumber(java.lang.CharSequence number)
isPossibleNumberWithReason(com.google.i18n.phonenumbers.Phonenumber.PhoneNumber)
and
isValidNumber(com.google.i18n.phonenumbers.Phonenumber.PhoneNumber)
should be used.number
- the number that needs to be checkedpublic boolean isPossibleNumber(Phonenumber.PhoneNumber number)
isPossibleNumberWithReason(com.google.i18n.phonenumbers.Phonenumber.PhoneNumber)
. Instead of returning the reason
for failure, this method returns true if the number is either a possible fully-qualified number
(containing the area code and country code), or if the number could be a possible local number
(with a country code, but missing an area code). Local numbers are considered possible if they
could be possibly dialled in this format: if the area code is needed for a call to connect, the
number is not considered possible without it.number
- the number that needs to be checkedpublic boolean isPossibleNumberForType(Phonenumber.PhoneNumber number, PhoneNumberUtil.PhoneNumberType type)
isPossibleNumberForTypeWithReason(com.google.i18n.phonenumbers.Phonenumber.PhoneNumber, com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberType)
. Instead of returning the
reason for failure, this method returns true if the number is either a possible fully-qualified
number (containing the area code and country code), or if the number could be a possible local
number (with a country code, but missing an area code). Local numbers are considered possible
if they could be possibly dialled in this format: if the area code is needed for a call to
connect, the number is not considered possible without it.number
- the number that needs to be checkedtype
- the type we are interested inpublic PhoneNumberUtil.ValidationResult isPossibleNumberWithReason(Phonenumber.PhoneNumber number)
isValidNumber(com.google.i18n.phonenumbers.Phonenumber.PhoneNumber)
in the following sense:
number
- the number that needs to be checkedpublic PhoneNumberUtil.ValidationResult isPossibleNumberForTypeWithReason(Phonenumber.PhoneNumber number, PhoneNumberUtil.PhoneNumberType type)
getSupportedTypesForRegion(java.lang.String)
or getSupportedTypesForNonGeoEntity(int)
respectively before calling this method to determine whether you should call it for this number
at all.
This provides a more lenient check than isValidNumber(com.google.i18n.phonenumbers.Phonenumber.PhoneNumber)
in the following sense:
number
- the number that needs to be checkedtype
- the type we are interested inpublic boolean isPossibleNumber(java.lang.CharSequence number, java.lang.String regionDialingFrom)
isValidNumber(com.google.i18n.phonenumbers.Phonenumber.PhoneNumber)
. See #isPossibleNumber(PhoneNumber)
for details.
This method first parses the number, then invokes #isPossibleNumber(PhoneNumber)
with the resultant PhoneNumber object.
number
- the number that needs to be checkedregionDialingFrom
- the region that we are expecting the number to be dialed from.
Note this is different from the region where the number belongs. For example, the number
+1 650 253 0000 is a number that belongs to US. When written in this form, it can be
dialed from any region. When it is written as 00 1 650 253 0000, it can be dialed from any
region which uses an international dialling prefix of 00. When it is written as
650 253 0000, it can only be dialed from within the US, and when written as 253 0000, it
can only be dialed from within a smaller area in the US (Mountain View, CA, to be more
specific).public boolean truncateTooLongNumber(Phonenumber.PhoneNumber number)
number
- a PhoneNumber object which contains a number that is too long to be validpublic AsYouTypeFormatter getAsYouTypeFormatter(java.lang.String regionCode)
AsYouTypeFormatter
for the specific region.regionCode
- the region where the phone number is being enteredAsYouTypeFormatter
object, which can be used
to format phone numbers in the specific region "as you type"public Phonenumber.PhoneNumber parse(java.lang.CharSequence numberToParse, java.lang.String defaultRegion) throws NumberParseException
This method will throw a NumberParseException
if the
number is not considered to be a possible number. Note that validation of whether the number
is actually a valid number for a particular region is not performed. This can be done
separately with isValidNumber(com.google.i18n.phonenumbers.Phonenumber.PhoneNumber)
.
Note this method canonicalizes the phone number such that different representations can be
easily compared, no matter what form it was originally entered in (e.g. national,
international). If you want to record context about the number being parsed, such as the raw
input that was entered, how the country code was derived etc. then call parseAndKeepRawInput(java.lang.CharSequence, java.lang.String)
instead.
numberToParse
- number that we are attempting to parse. This can contain formatting such
as +, ( and -, as well as a phone number extension. It can also be provided in RFC3966
format.defaultRegion
- region that we are expecting the number to be from. This is only used if
the number being parsed is not written in international format. The country_code for the
number in this case would be stored as that of the default region supplied. If the number
is guaranteed to start with a '+' followed by the country calling code, then RegionCode.ZZ
or null can be supplied.NumberParseException
- if the string is not considered to be a viable phone number (e.g.
too few or too many digits) or if no default region was supplied and the number is not in
international format (does not start with +)public void parse(java.lang.CharSequence numberToParse, java.lang.String defaultRegion, Phonenumber.PhoneNumber phoneNumber) throws NumberParseException
parse(CharSequence, String)
, but accepts mutable PhoneNumber as a
parameter to decrease object creation when invoked many times.NumberParseException
public Phonenumber.PhoneNumber parseAndKeepRawInput(java.lang.CharSequence numberToParse, java.lang.String defaultRegion) throws NumberParseException
parse(java.lang.CharSequence, java.lang.String)
in that it always populates the raw_input field of the protocol buffer with numberToParse as
well as the country_code_source field.numberToParse
- number that we are attempting to parse. This can contain formatting such
as +, ( and -, as well as a phone number extension.defaultRegion
- region that we are expecting the number to be from. This is only used if
the number being parsed is not written in international format. The country calling code
for the number in this case would be stored as that of the default region supplied.NumberParseException
- if the string is not considered to be a viable phone number or if
no default region was suppliedpublic void parseAndKeepRawInput(java.lang.CharSequence numberToParse, java.lang.String defaultRegion, Phonenumber.PhoneNumber phoneNumber) throws NumberParseException
parseAndKeepRawInput(CharSequence, String)
, but accepts a mutable
PhoneNumber as a parameter to decrease object creation when invoked many times.NumberParseException
public java.lang.Iterable<PhoneNumberMatch> findNumbers(java.lang.CharSequence text, java.lang.String defaultRegion)
PhoneNumberMatches
in text
. This
is a shortcut for getMatcher(text, defaultRegion, Leniency.VALID, Long.MAX_VALUE)
.text
- the text to search for phone numbers, null for no textdefaultRegion
- region that we are expecting the number to be from. This is only used if
the number being parsed is not written in international format. The country_code for the
number in this case would be stored as that of the default region supplied. May be null if
only international numbers are expected.public java.lang.Iterable<PhoneNumberMatch> findNumbers(java.lang.CharSequence text, java.lang.String defaultRegion, PhoneNumberUtil.Leniency leniency, long maxTries)
PhoneNumberMatches
in text
.text
- the text to search for phone numbers, null for no textdefaultRegion
- region that we are expecting the number to be from. This is only used if
the number being parsed is not written in international format. The country_code for the
number in this case would be stored as that of the default region supplied. May be null if
only international numbers are expected.leniency
- the leniency to use when evaluating candidate phone numbersmaxTries
- the maximum number of invalid numbers to try before giving up on the text.
This is to cover degenerate cases where the text has a lot of false positives in it. Must
be >= 0
.public PhoneNumberUtil.MatchType isNumberMatch(Phonenumber.PhoneNumber firstNumberIn, Phonenumber.PhoneNumber secondNumberIn)
Returns EXACT_MATCH if the country_code, NSN, presence of a leading zero for Italian numbers and any extension present are the same. Returns NSN_MATCH if either or both has no region specified, and the NSNs and extensions are the same. Returns SHORT_NSN_MATCH if either or both has no region specified, or the region specified is the same, and one NSN could be a shorter version of the other number. This includes the case where one has an extension specified, and the other does not. Returns NO_MATCH otherwise. For example, the numbers +1 345 657 1234 and 657 1234 are a SHORT_NSN_MATCH. The numbers +1 345 657 1234 and 345 657 are a NO_MATCH.
firstNumberIn
- first number to comparesecondNumberIn
- second number to comparepublic PhoneNumberUtil.MatchType isNumberMatch(java.lang.CharSequence firstNumber, java.lang.CharSequence secondNumber)
#isNumberMatch(PhoneNumber, PhoneNumber)
. No default region is known.firstNumber
- first number to compare. Can contain formatting, and can have country
calling code specified with + at the start.secondNumber
- second number to compare. Can contain formatting, and can have country
calling code specified with + at the start.#isNumberMatch(PhoneNumber, PhoneNumber)
for more details.public PhoneNumberUtil.MatchType isNumberMatch(Phonenumber.PhoneNumber firstNumber, java.lang.CharSequence secondNumber)
#isNumberMatch(PhoneNumber, PhoneNumber)
. No default region is known.firstNumber
- first number to compare in proto buffer formatsecondNumber
- second number to compare. Can contain formatting, and can have country
calling code specified with + at the start.#isNumberMatch(PhoneNumber, PhoneNumber)
for more details.public boolean canBeInternationallyDialled(Phonenumber.PhoneNumber number)
number
- the phone-number for which we want to know whether it is diallable from
outside the regionpublic boolean isMobileNumberPortableRegion(java.lang.String regionCode)
regionCode
- the region for which we want to know whether it supports mobile number
portability or notCopyright © 2018 Google. All Rights Reserved.