parse method

PhoneNumber parse(
  1. String? numberToParse,
  2. String? defaultRegion
)

Parses a string and returns it as a phone number in proto buffer format. The method is quite lenient and looks for a number in the input text (raw input) and does not check whether the string is definitely only a phone number. To do this, it ignores punctuation and white-space, as well as any text before the number (e.g. a leading 'Tel: ') and trims the non-number bits. It will accept a number in any format (E164, national, international etc), assuming it can be interpreted with the defaultRegion supplied. It also attempts to convert any alpha characters into digits if it thinks this is a vanity number of the type '1800 MICROSOFT'.

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() instead.

This method will throw a phonenumbers.Error 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.

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 countryCode 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 PhoneNumber a phone number proto buffer filled with the parsed number. Throws 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 +).

Implementation

PhoneNumber parse(String? numberToParse, String? defaultRegion) {
  return _parseHelper(numberToParse, defaultRegion, false, true);
}