Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## com.google.i18n.phonenumbers
Class PhoneNumberUtil

    java.lang.Object
      com.google.i18n.phonenumbers.PhoneNumberUtil

    public class PhoneNumberUtil

extends java.lang.Object

Utility for international phone numbers. Functionality includes formatting, parsing and validation.

If you use this library, and want to be notified about important changes, please sign up to our mailing list.

Nested Class Summary
`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.
`static class` | `PhoneNumberUtil.PhoneNumberType` Type of phone numbers.
`static class` | `PhoneNumberUtil.ValidationResult` Possible outcomes when testing if a PhoneNumber is possible.

Method Summary
`static java.lang.String` | `convertAlphaCharactersInNumber(java.lang.String number)` Converts all alpha characters in a number to their respective digits on a keypad, but retains existing formatting.
`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 number, PhoneNumberUtil.PhoneNumberFormat numberFormat)` Formats a phone number in the specified format using default rules.
`void` | `format(PhoneNumber number, PhoneNumberUtil.PhoneNumberFormat numberFormat, java.lang.StringBuilder formattedNumber)`
`java.lang.String` | `formatByPattern(PhoneNumber number, PhoneNumberUtil.PhoneNumberFormat numberFormat, java.util.List<NumberFormat> userDefinedFormats)` Formats a phone number in the specified format using client-defined formatting rules.
`java.lang.String` | `formatInOriginalFormat(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 number, java.lang.String carrierCode)` Formats a phone number in national format for dialing using the carrier as specified in the `carrierCode`.
`java.lang.String` | `formatNationalNumberWithPreferredCarrierCode(PhoneNumber number, java.lang.String 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` | `formatOutOfCountryCallingNumber(PhoneNumber number, java.lang.String regionCallingFrom)` Formats a phone number for out-of-country dialing purposes.
`java.lang.String` | `formatOutOfCountryKeepingAlphaChars(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.
`PhoneNumber` | `getExampleNumber(java.lang.String regionCode)` Gets a valid number for the specified region.
`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.
`int` | `getLengthOfGeographicalAreaCode(PhoneNumber number)` Gets the length of the geographical area code in the `nationalNumber_` field of 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 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 number)` Gets the national significant number of the 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 number)` Gets the type of a phone number.
`java.lang.String` | `getRegionCodeForCountryCode(int countryCallingCode)` Returns the region code that matches the specific country calling code.
`java.lang.String` | `getRegionCodeForNumber(PhoneNumber number)` Returns the region where a phone number is from.
`java.util.Set<java.lang.String>` | `getSupportedRegions()` Convenience method to enable tests to get a list of what regions the library has metadata for.
`boolean` | `isAlphaNumber(java.lang.String number)` Checks if the number is a valid vanity (alpha) number such as 800 MICROSOFT.
`boolean` | `isNANPACountry(java.lang.String regionCode)` Checks if this is a region under the North American Numbering Plan Administration (NANPA).
`PhoneNumberUtil.MatchType` | `isNumberMatch(PhoneNumber firstNumberIn, PhoneNumber secondNumberIn)` Takes two phone numbers and compares them for equality.
`PhoneNumberUtil.MatchType` | `isNumberMatch(PhoneNumber firstNumber, java.lang.String secondNumber)` Takes two phone numbers and compares them for equality.
`boolean` | `isPossibleNumber(PhoneNumber number)` Convenience wrapper around `isPossibleNumberWithReason(PhoneNumber)`.
`boolean` | `isPossibleNumber(java.lang.String 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.
`PhoneNumberUtil.ValidationResult` | `isPossibleNumberWithReason(PhoneNumber number)` Check whether a phone number is a possible number.
`boolean` | `isValidNumber(PhoneNumber number)` Tests whether a phone number matches a valid pattern.
`boolean` | `isValidNumberForRegion(PhoneNumber number, java.lang.String regionCode)` Tests whether a phone number is valid for a certain region.
`static java.lang.String` | `normalizeDigitsOnly(java.lang.String number)` Normalizes a string of characters representing a phone number.
`PhoneNumber` | `parse(java.lang.String numberToParse, java.lang.String defaultRegion)` Parses a string and returns it in proto buffer format.
`void` | `parse(java.lang.String numberToParse, java.lang.String defaultRegion, PhoneNumber phoneNumber)`
`PhoneNumber` | `parseAndKeepRawInput(java.lang.String numberToParse, java.lang.String defaultRegion)` Parses a string and returns it in proto buffer format.
`void` | `parseAndKeepRawInput(java.lang.String numberToParse, java.lang.String defaultRegion, PhoneNumber phoneNumber)`
`boolean` | `truncateTooLongNumber(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.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Method Detail

### normalizeDigitsOnly

    public static java.lang.String normalizeDigitsOnly(java.lang.String number)

  Normalizes a string of characters representing a phone number. This converts wide-ascii and arabic-indic numerals to European numerals, and strips punctuation and alpha characters.

  Parameters:
    `number` - a string of characters representing a phone number
  Returns:
    the normalized string version of the phone number

### convertAlphaCharactersInNumber

    public static java.lang.String convertAlphaCharactersInNumber(java.lang.String number)

  Converts all alpha characters in a number to their respective digits on a keypad, but retains existing formatting. This Java implementation of this function also converts wide-ascii digits to normal ascii digits, and converts Arabic-Indic numerals to European numerals.

### getLengthOfGeographicalAreaCode

    public int getLengthOfGeographicalAreaCode(PhoneNumber number)

  Gets the length of the geographical area code in the `nationalNumber_` field of the PhoneNumber object passed in, so that clients could use it to split a national significant number into geographical area code and subscriber number. It works in such a way that the resultant subscriber number should be diallable, at least on some devices. An example of how this could be used:

       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. Read the following carefully before deciding to use this method: - geographical area codes change over time, and this method honors those changes; therefore, it doesn't guarantee the stability of the result it produces. - subscriber numbers may not be diallable from all devices (notably mobile devices, which typically requires the full national_number to be dialled in most countries). - most non-geographical numbers have no area codes. - some geographical numbers have no area codes.

  Parameters:
    `number` - the PhoneNumber object for which clients want to know the length of the area code.
  Returns:
    the length of area code of the PhoneNumber object passed in.

### getLengthOfNationalDestinationCode

    public int getLengthOfNationalDestinationCode(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. The NDC of a phone number is normally the first group of digit(s) right after the country calling code when the number is formatted in the international format, if there is a subscriber number part that follows. An example of how this could be used: 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()`.

  Parameters:
    `number` - the PhoneNumber object for which clients want to know the length of the NDC.
  Returns:
    the length of NDC of the PhoneNumber object passed in.

### getSupportedRegions

    public java.util.Set<java.lang.String> getSupportedRegions()

  Convenience method to enable tests to get a list of what regions the library has metadata for.

### getInstance

    public static PhoneNumberUtil getInstance()

  Gets a `PhoneNumberUtil` instance to carry out international phone number formatting, parsing, or validation. The instance is loaded with phone number metadata for a number of most commonly used regions.

  The `PhoneNumberUtil` is implemented as a singleton. Therefore, calling getInstance multiple times will only result in one instance being created.

  Returns:
    a PhoneNumberUtil instance

### format

    public java.lang.String format(PhoneNumber number,
                                   PhoneNumberUtil.PhoneNumberFormat numberFormat)

  Formats a phone number in the specified format using default rules. Note that this does not promise to produce a phone number that the user can dial from where they are - although we do format in either 'national' or 'international' format depending on what the client asks for, we do not currently support a more abbreviated format, such as for users in the same "area" who could potentially dial the number without area code. Note that if the phone number has a country calling code of 0 or an otherwise invalid country calling code, we cannot work out which formatting rules to apply so we return the national significant number with no formatting applied.

  Parameters:
    `number` - the phone number to be formatted
    `numberFormat` - the format the phone number should be formatted into
  Returns:
    the formatted phone number

### format

    public void format(PhoneNumber number,
                       PhoneNumberUtil.PhoneNumberFormat numberFormat,
                       java.lang.StringBuilder formattedNumber)

### formatByPattern

    public java.lang.String formatByPattern(PhoneNumber number,
                                            PhoneNumberUtil.PhoneNumberFormat numberFormat,
                                            java.util.List<NumberFormat> userDefinedFormats)

  Formats a phone number in the specified format using client-defined formatting rules. Note that if the phone number has a country calling code of zero or an otherwise invalid country calling code, we cannot work out things like whether there should be a national prefix applied, or how to format extensions, so we return the national significant number with no formatting applied.

  Parameters:
    `number` - the phone number to be formatted
    `numberFormat` - the format the phone number should be formatted into
    `userDefinedFormats` - formatting rules specified by clients
  Returns:
    the formatted phone number

### formatNationalNumberWithCarrierCode

    public java.lang.String formatNationalNumberWithCarrierCode(PhoneNumber number,
                                                                java.lang.String carrierCode)

  Formats a phone number in national format for dialing using the carrier as specified in the `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.

  Parameters:
    `number` - the phone number to be formatted
    `carrierCode` - the carrier selection code to be used
  Returns:
    the formatted phone number in national format for dialing using the carrier as specified in the `carrierCode`

### formatNationalNumberWithPreferredCarrierCode

    public java.lang.String formatNationalNumberWithPreferredCarrierCode(PhoneNumber number,
                                                                         java.lang.String 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. If that is missing, use the `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(PhoneNumber, java.lang.String)` instead if the carrier code passed in should take precedence over the number's `preferredDomesticCarrierCode` when formatting.

  Parameters:
    `number` - the phone number to be formatted
    `fallbackCarrierCode` - the carrier selection code to be used, if none is found in the phone number itself
  Returns:
    the formatted phone number in national format for dialing using the number's `preferredDomesticCarrierCode`, or the `fallbackCarrierCode` passed in if none is found

### formatOutOfCountryCallingNumber

    public java.lang.String formatOutOfCountryCallingNumber(PhoneNumber number,
                                                            java.lang.String regionCallingFrom)

  Formats a phone number for out-of-country dialing purposes. If no regionCallingFrom is supplied, we format the number in its INTERNATIONAL format. If the country calling code is the same as the region where the number is from, then NATIONAL formatting will be applied.

  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.

  Parameters:
    `number` - the phone number to be formatted
    `regionCallingFrom` - the ISO 3166-1 two-letter region code that denotes the region where the call is being placed
  Returns:
    the formatted phone number

### formatInOriginalFormat

    public java.lang.String formatInOriginalFormat(PhoneNumber number,
                                                   java.lang.String regionCallingFrom)

  Formats a phone number using the original phone number format that the number is parsed from. The original format is embedded in the country_code_source field of the PhoneNumber object passed in. If such information is missing, the number will be formatted into the NATIONAL format by default.

  Parameters:
    `number` - the phone number that needs to be formatted in its original number format
    `regionCallingFrom` - the region whose IDD needs to be prefixed if the original number has one
  Returns:
    the formatted phone number in its original number format

### formatOutOfCountryKeepingAlphaChars

    public java.lang.String formatOutOfCountryKeepingAlphaChars(PhoneNumber number,
                                                                java.lang.String regionCallingFrom)

  Formats a phone number for out-of-country dialing purposes. Note that in this version, if the number was entered originally using alpha characters and this version of the number is stored in raw_input, this representation of the number will be used rather than the digit representation. Grouping information, as specified by characters such as "-" and " ", will be retained.

  Caveats:

  - This will not produce good results if the country calling code is both present in the raw input _and_ is the start of the national number. This is not a problem in the regions which typically use alpha numbers.
  - This will also not produce good results if the raw input has any grouping information within the first three digits of the national number, and if the function needs to strip preceding digits/words in the raw input before these digits. Normally people group the first three digits together so this is not a huge problem - and will be fixed if it proves to be so.

  Parameters:
    `number` - the phone number that needs to be formatted
    `regionCallingFrom` - the region where the call is being placed
  Returns:
    the formatted phone number

### getNationalSignificantNumber

    public java.lang.String getNationalSignificantNumber(PhoneNumber number)

  Gets the national significant number of the a phone number. Note a national significant number doesn't contain a national prefix or any formatting.

  Parameters:
    `number` - the PhoneNumber object for which the national significant number is needed
  Returns:
    the national significant number of the PhoneNumber object passed in

### getExampleNumber

    public PhoneNumber getExampleNumber(java.lang.String regionCode)

  Gets a valid number for the specified region.

  Parameters:
    `regionCode` - the ISO 3166-1 two-letter region code that denotes the region for which an example number is needed
  Returns:
    a valid fixed-line number for the specified region. Returns null when the metadata does not contain such information.

### getExampleNumberForType

    public PhoneNumber getExampleNumberForType(java.lang.String regionCode,
                                               PhoneNumberUtil.PhoneNumberType type)

  Gets a valid number for the specified region and number type.

  Parameters:
    `regionCode` - the ISO 3166-1 two-letter region code that denotes the region for which an example number is needed
    `type` - the type of number that is needed
  Returns:
    a valid number for the specified region and type. Returns null when the metadata does not contain such information.

### getNumberType

    public PhoneNumberUtil.PhoneNumberType getNumberType(PhoneNumber number)

  Gets the type of a phone number.

  Parameters:
    `number` - the phone number that we want to know the type
  Returns:
    the type of the phone number

### isValidNumber

    public boolean isValidNumber(PhoneNumber number)

  Tests whether a phone number matches a valid pattern. Note this doesn't verify the number is actually in use, which is impossible to tell by just looking at a number itself.

  Parameters:
    `number` - the phone number that we want to validate
  Returns:
    a boolean that indicates whether the number is of a valid pattern

### isValidNumberForRegion

    public boolean isValidNumberForRegion(PhoneNumber number,
                                          java.lang.String regionCode)

  Tests whether a phone number is valid for a certain region. Note this doesn't verify the number is actually in use, which is impossible to tell by just looking at a number itself. If the country calling code is not the same as the country calling code for the region, this immediately exits with false. After this, the specific number pattern rules for the region are examined. This is useful for determining for example whether a particular number is valid for Canada, rather than just a valid NANPA number.

  Parameters:
    `number` - the phone number that we want to validate
    `regionCode` - the ISO 3166-1 two-letter region code that denotes the region that we want to validate the phone number for
  Returns:
    a boolean that indicates whether the number is of a valid pattern

### getRegionCodeForNumber

    public java.lang.String getRegionCodeForNumber(PhoneNumber number)

  Returns the region where a phone number is from. This could be used for geocoding at the region level.

  Parameters:
    `number` - the phone number whose origin we want to know
  Returns:
    the region where the phone number is from, or null if no region matches this calling code

### getRegionCodeForCountryCode

    public java.lang.String getRegionCodeForCountryCode(int countryCallingCode)

  Returns the region code that matches the specific country calling code. In the case of no region code being found, ZZ will be returned. In the case of multiple regions, the one designated in the metadata as the "main" region for this calling code will be returned.

### getCountryCodeForRegion

    public int getCountryCodeForRegion(java.lang.String regionCode)

  Returns the country calling code for a specific region. For example, this would be 1 for the United States, and 64 for New Zealand.

  Parameters:
    `regionCode` - the ISO 3166-1 two-letter region code that denotes the region that we want to get the country calling code for
  Returns:
    the country calling code for the region denoted by regionCode

### getNddPrefixForRegion

    public java.lang.String getNddPrefixForRegion(java.lang.String regionCode,
                                                  boolean stripNonDigits)

  Returns the national dialling prefix for a specific region. For example, this would be 1 for the United States, and 0 for New Zealand. Set stripNonDigits to true to strip symbols like "~" (which indicates a wait for a dialling tone) from the prefix returned. If no national prefix is present, we return null.

  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.

  Parameters:
    `regionCode` - the ISO 3166-1 two-letter region code that denotes the region that we want to get the dialling prefix for
    `stripNonDigits` - true to strip non-digits from the national dialling prefix
  Returns:
    the dialling prefix for the region denoted by regionCode

### isNANPACountry

    public boolean isNANPACountry(java.lang.String regionCode)

  Checks if this is a region under the North American Numbering Plan Administration (NANPA).

  Returns:
    true if regionCode is one of the regions under NANPA

### isAlphaNumber

    public boolean isAlphaNumber(java.lang.String number)

  Checks if the number is a valid vanity (alpha) number such as 800 MICROSOFT. A valid vanity number will start with at least 3 digits and will have three or more alpha characters. This does not do region-specific checks - to work out if this number is actually valid for a region, it should be parsed and methods such as `isPossibleNumberWithReason(PhoneNumber)` and `isValidNumber(PhoneNumber)` should be used.

  Parameters:
    `number` - the number that needs to be checked
  Returns:
    true if the number is a valid vanity number

### isPossibleNumber

    public boolean isPossibleNumber(PhoneNumber number)

  Convenience wrapper around `isPossibleNumberWithReason(PhoneNumber)`. Instead of returning the reason for failure, this method returns a boolean value.

  Parameters:
    `number` - the number that needs to be checked
  Returns:
    true if the number is possible

### isPossibleNumberWithReason

    public PhoneNumberUtil.ValidationResult isPossibleNumberWithReason(PhoneNumber number)

  Check whether a phone number is a possible number. It provides a more lenient check than `isValidNumber(PhoneNumber)` in the following sense:
  1. It only checks the length of phone numbers. In particular, it doesn't check starting digits of the number.
  2. It doesn't attempt to figure out the type of the number, but uses general rules which applies to all types of phone numbers in a region. Therefore, it is much faster than isValidNumber.
  3. For fixed line numbers, many regions have the concept of area code, which together with subscriber number constitute the national significant number. It is sometimes okay to dial the subscriber number only when dialing in the same area. This function will return true if the subscriber-number-only version is passed in. On the other hand, because isValidNumber validates using information on both starting digits (for fixed line numbers, that would most likely be area codes) and length (obviously includes the length of area codes for fixed line numbers), it will return false for the subscriber-number-only version.
  Parameters:
    `number` - the number that needs to be checked
  Returns:
    a ValidationResult object which indicates whether the number is possible

### isPossibleNumber

    public boolean isPossibleNumber(java.lang.String 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. It provides a more lenient check than `isValidNumber(PhoneNumber)`. See `isPossibleNumber(PhoneNumber)` for details.

  This method first parses the number, then invokes `isPossibleNumber(PhoneNumber number)` with the resultant PhoneNumber object.

  Parameters:
    `number` - the number that needs to be checked, in the form of a string
    `regionDialingFrom` - the ISO 3166-1 two-letter region code that denotes 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).
  Returns:
    true if the number is possible

### truncateTooLongNumber

    public boolean truncateTooLongNumber(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. If no valid number could be extracted, the PhoneNumber object passed in will not be modified.

  Parameters:
    `number` - a PhoneNumber object which contains a number that is too long to be valid.
  Returns:
    true if a valid phone number can be successfully extracted.

### getAsYouTypeFormatter

    public AsYouTypeFormatter getAsYouTypeFormatter(java.lang.String regionCode)

  Gets an `AsYouTypeFormatter` for the specific region.

  Parameters:
    `regionCode` - the ISO 3166-1 two-letter region code that denotes the region where the phone number is being entered
  Returns:
    an `AsYouTypeFormatter` object, which can be used to format phone numbers in the specific region "as you type"

### parse

    public PhoneNumber parse(java.lang.String numberToParse,
                             java.lang.String defaultRegion)
                      throws NumberParseException

  Parses a string and returns it in proto buffer format. 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(PhoneNumber)`.

  Parameters:
    `numberToParse` - number that we are attempting to parse. This can contain formatting such as +, ( and -, as well as a phone number extension.
    `defaultRegion` - the ISO 3166-1 two-letter region code that denotes the 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 "ZZ" or null can be supplied.
  Returns:
    a phone number proto buffer filled with the parsed number
  Throws:
    `NumberParseException` - if the string is not considered to be a viable phone number or if no default region was supplied and the number is not in international format (does not start with +)

### parse

    public void parse(java.lang.String numberToParse,
                      java.lang.String defaultRegion,
                      PhoneNumber phoneNumber)
               throws NumberParseException

  Throws:
    `NumberParseException`

### parseAndKeepRawInput

    public PhoneNumber parseAndKeepRawInput(java.lang.String numberToParse,
                                            java.lang.String defaultRegion)
                                     throws NumberParseException

  Parses a string and returns it in proto buffer format. This method differs from `parse(java.lang.String, 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.

  Parameters:
    `numberToParse` - number that we are attempting to parse. This can contain formatting such as +, ( and -, as well as a phone number extension.
    `defaultRegion` - the ISO 3166-1 two-letter region code that denotes the 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.
  Returns:
    a phone number proto buffer filled with the parsed number
  Throws:
    `NumberParseException` - if the string is not considered to be a viable phone number or if no default region was supplied

### parseAndKeepRawInput

    public void parseAndKeepRawInput(java.lang.String numberToParse,
                                     java.lang.String defaultRegion,
                                     PhoneNumber phoneNumber)
                              throws NumberParseException

  Throws:
    `NumberParseException`

### findNumbers

    public java.lang.Iterable<PhoneNumberMatch> findNumbers(java.lang.CharSequence text,
                                                            java.lang.String defaultRegion)

  Returns an iterable over all `PhoneNumberMatches` in `text`. This is a shortcut for `getMatcher(text, defaultRegion, Leniency.VALID, Long.MAX_VALUE)`.

  Parameters:
    `text` - the text to search for phone numbers, null for no text
    `defaultRegion` - the ISO 3166-1 two-letter region code that denotes the 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. May be null if only international numbers are expected.

### findNumbers

    public 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`.

  Parameters:
    `text` - the text to search for phone numbers, null for no text
    `defaultRegion` - the ISO 3166-1 two-letter region code that denotes the 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. May be null if only international numbers are expected.
    `leniency` - the leniency to use when evaluating candidate phone numbers
    `maxTries` - 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`.

### isNumberMatch

    public PhoneNumberUtil.MatchType isNumberMatch(PhoneNumber firstNumberIn,
                                                   PhoneNumber secondNumberIn)

  Takes two phone numbers and compares them for equality.

  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.

  Parameters:
    `firstNumberIn` - first number to compare
    `secondNumberIn` - second number to compare
  Returns:
    NO_MATCH, SHORT_NSN_MATCH, NSN_MATCH or EXACT_MATCH depending on the level of equality of the two numbers, described in the method definition.

### isNumberMatch

    public PhoneNumberUtil.MatchType isNumberMatch(PhoneNumber firstNumber,
                                                   java.lang.String secondNumber)

  Takes two phone numbers and compares them for equality. This is a convenience wrapper for isNumberMatch(PhoneNumber firstNumber, PhoneNumber secondNumber). No default region is known.

  Parameters:
    `firstNumber` - first number to compare in proto buffer format.
    `secondNumber` - second number to compare. Can contain formatting, and can have country calling code specified with + at the start.
  Returns:
    NOT_A_NUMBER, NO_MATCH, SHORT_NSN_MATCH, NSN_MATCH, EXACT_MATCH. See isNumberMatch(PhoneNumber firstNumber, PhoneNumber secondNumber) for more details.

Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD