本文整理汇总了C#中PhoneNumbers.PhoneNumber类的典型用法代码示例。如果您正苦于以下问题:C# PhoneNumber类的具体用法?C# PhoneNumber怎么用?C# PhoneNumber使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
PhoneNumber类属于PhoneNumbers命名空间,在下文中一共展示了PhoneNumber类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: PhoneNumberMatch
public PhoneNumberMatch(int start, String rawString, PhoneNumber number)
{
if (start < 0)
throw new ArgumentException("Start index must be >= 0.");
if (rawString == null || number == null)
throw new ArgumentNullException();
Start = start;
RawString = rawString;
Number = number;
}
示例2: GetNationalSignificantNumber
/**
* Gets the national significant number of the a phone number. Note a national significant number
* doesn't contain a national prefix or any formatting.
*
* @param number the PhoneNumber object for which the national significant number is needed
* @return the national significant number of the PhoneNumber object passed in
*/
public String GetNationalSignificantNumber(PhoneNumber number)
{
// If a leading zero has been set, we prefix this now. Note this is not a national prefix.
StringBuilder nationalNumber = new StringBuilder(number.ItalianLeadingZero ? "0" : "");
nationalNumber.Append(number.NationalNumber);
return nationalNumber.ToString();
}
示例3: FormatNationalNumberWithPreferredCarrierCode
/**
* 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 {@code fallbackCarrierCode} passed in instead. If there is no
* {@code preferredDomesticCarrierCode}, and the {@code fallbackCarrierCode} contains an empty
* string, return the number in national format without any carrier code.
*
* <p>Use {@link #formatNationalNumberWithCarrierCode} instead if the carrier code passed in
* should take precedence over the number's {@code preferredDomesticCarrierCode} when formatting.
*
* @param number the phone number to be formatted
* @param fallbackCarrierCode the carrier selection code to be used, if none is found in the
* phone number itself
* @return the formatted phone number in national format for dialing using the number's
* {@code preferredDomesticCarrierCode}, or the {@code fallbackCarrierCode} passed in if
* none is found
*/
public String FormatNationalNumberWithPreferredCarrierCode(PhoneNumber number,
String fallbackCarrierCode)
{
return FormatNationalNumberWithCarrierCode(number, number.HasPreferredDomesticCarrierCode
? number.PreferredDomesticCarrierCode
: fallbackCarrierCode);
}
示例4: FormatInOriginalFormat
/**
* 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. When the number contains a leading zero and this is unexpected for this
* country, or we don't have a formatting pattern for the number, the method returns the raw input
* when it is available.
*
* Note this method guarantees no digit will be inserted, removed or modified as a result of
* formatting.
*
* @param number the phone number that needs to be formatted in its original number format
* @param regionCallingFrom the region whose IDD needs to be prefixed if the original number
* has one
* @return the formatted phone number in its original number format
*/
public String FormatInOriginalFormat(PhoneNumber number, String regionCallingFrom)
{
if (number.HasRawInput &&
(HasUnexpectedItalianLeadingZero(number) || !HasFormattingPatternForNumber(number)))
{
// We check if we have the formatting pattern because without that, we might format the number
// as a group without national prefix.
return number.RawInput;
}
if (!number.HasCountryCodeSource)
return Format(number, PhoneNumberFormat.NATIONAL);
String formattedNumber;
switch (number.CountryCodeSource)
{
case CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN:
formattedNumber = Format(number, PhoneNumberFormat.INTERNATIONAL);
break;
case CountryCodeSource.FROM_NUMBER_WITH_IDD:
formattedNumber = FormatOutOfCountryCallingNumber(number, regionCallingFrom);
break;
case CountryCodeSource.FROM_NUMBER_WITHOUT_PLUS_SIGN:
formattedNumber = Format(number, PhoneNumberFormat.INTERNATIONAL).Substring(1);
break;
case CountryCodeSource.FROM_DEFAULT_COUNTRY:
// Fall-through to default case.
default:
String regionCode = GetRegionCodeForCountryCode(number.CountryCode);
// We strip non-digits from the NDD here, and from the raw input later, so that we can
// compare them easily.
String nationalPrefix = GetNddPrefixForRegion(regionCode, true /* strip non-digits */);
String nationalFormat = Format(number, PhoneNumberFormat.NATIONAL);
if (nationalPrefix == null || nationalPrefix.Length == 0)
{
// If the region doesn't have a national prefix at all, we can safely return the national
// format without worrying about a national prefix being added.
formattedNumber = nationalFormat;
break;
}
// Otherwise, we check if the original number was entered with a national prefix.
if (RawInputContainsNationalPrefix(
number.RawInput, nationalPrefix, regionCode))
{
// If so, we can safely return the national format.
formattedNumber = nationalFormat;
break;
}
PhoneMetadata metadata = GetMetadataForRegion(regionCode);
String nationalNumber = GetNationalSignificantNumber(number);
NumberFormat formatRule =
ChooseFormattingPatternForNumber(metadata.NumberFormatList, nationalNumber);
// When the format we apply to this number doesn't contain national prefix, we can just
// return the national format.
// TODO: Refactor the code below with the code in isNationalPrefixPresentIfRequired.
String candidateNationalPrefixRule = formatRule.NationalPrefixFormattingRule;
// We assume that the first-group symbol will never be _before_ the national prefix.
int indexOfFirstGroup = candidateNationalPrefixRule.IndexOf("${1}");
if (indexOfFirstGroup <= 0)
{
formattedNumber = nationalFormat;
break;
}
candidateNationalPrefixRule =
candidateNationalPrefixRule.Substring(0, indexOfFirstGroup);
candidateNationalPrefixRule = NormalizeDigitsOnly(candidateNationalPrefixRule);
if (candidateNationalPrefixRule.Length == 0)
{
// National prefix not used when formatting this number.
formattedNumber = nationalFormat;
break;
}
// Otherwise, we need to remove the national prefix from our output.
var numFormatCopy = new NumberFormat.Builder()
.MergeFrom(formatRule)
.ClearNationalPrefixFormattingRule()
.Build();
List<NumberFormat> numberFormats = new List<NumberFormat>(1);
numberFormats.Add(numFormatCopy);
formattedNumber = FormatByPattern(number, PhoneNumberFormat.NATIONAL, numberFormats);
break;
}
String rawInput = number.RawInput;
// If no digit is inserted/removed/modified as a result of our formatting, we return the
//.........这里部分代码省略.........
示例5: Format
/**
* Same as {@link #format(PhoneNumber, PhoneNumberFormat)}, but accepts a mutable StringBuilder as
* a parameter to decrease object creation when invoked many times.
*/
public void Format(PhoneNumber number, PhoneNumberFormat numberFormat,
StringBuilder formattedNumber)
{
// Clear the StringBuilder first.
formattedNumber.Length = 0;
var countryCallingCode = number.CountryCode;
var nationalSignificantNumber = GetNationalSignificantNumber(number);
if (numberFormat == PhoneNumberFormat.E164)
{
// Early exit for E164 case since no formatting of the national number needs to be applied.
// Extensions are not formatted.
formattedNumber.Append(nationalSignificantNumber);
PrefixNumberWithCountryCallingCode(countryCallingCode, PhoneNumberFormat.E164,
formattedNumber);
return;
}
// Note getRegionCodeForCountryCode() is used because formatting information for regions which
// share a country calling code is contained by only one region for performance reasons. For
// example, for NANPA regions it will be contained in the metadata for US.
var regionCode = GetRegionCodeForCountryCode(countryCallingCode);
if (!HasValidCountryCallingCode(countryCallingCode))
{
formattedNumber.Append(nationalSignificantNumber);
return;
}
PhoneMetadata metadata = GetMetadataForRegionOrCallingCode(countryCallingCode, regionCode);
formattedNumber.Append(FormatNsn(nationalSignificantNumber, metadata, numberFormat));
MaybeAppendFormattedExtension(number, metadata, numberFormat, formattedNumber);
PrefixNumberWithCountryCallingCode(countryCallingCode, numberFormat, formattedNumber);
}
示例6: CanBeInternationallyDialled
/**
* Returns true if the number can be dialled from outside the region, or unknown. If the number
* can only be dialled from within the region, returns false. Does not check the number is a valid
* number.
* TODO: Make this method public when we have enough metadata to make it worthwhile.
*
* @param number the phone-number for which we want to know whether it is only diallable from
* outside the region
*/
public bool CanBeInternationallyDialled(PhoneNumber number)
{
String regionCode = GetRegionCodeForNumber(number);
if (!IsValidRegionCode(regionCode))
// Note numbers belonging to non-geographical entities (e.g. +800 numbers) are always
// internationally diallable, and will be caught here.
return true;
PhoneMetadata metadata = GetMetadataForRegion(regionCode);
String nationalSignificantNumber = GetNationalSignificantNumber(number);
return !IsNumberMatchingDesc(nationalSignificantNumber, metadata.NoInternationalDialling);
}
示例7: MaybeAppendFormattedExtension
/**
* Appends the formatted extension of a phone number to formattedNumber, if the phone number had
* an extension specified.
*/
private void MaybeAppendFormattedExtension(PhoneNumber number, PhoneMetadata metadata,
PhoneNumberFormat numberFormat, StringBuilder formattedNumber)
{
if (number.HasExtension && number.Extension.Length > 0)
{
if (numberFormat == PhoneNumberFormat.RFC3966)
{
formattedNumber.Append(RFC3966_EXTN_PREFIX).Append(number.Extension);
}
else
{
if (metadata.HasPreferredExtnPrefix)
formattedNumber.Append(metadata.PreferredExtnPrefix).Append(number.Extension);
else
formattedNumber.Append(DEFAULT_EXTN_PREFIX).Append(number.Extension);
}
}
}
示例8: HasUnexpectedItalianLeadingZero
/**
* Returns true if a number is from a region whose national significant number couldn't contain a
* leading zero, but has the italian_leading_zero field set to true.
*/
private bool HasUnexpectedItalianLeadingZero(PhoneNumber number)
{
return number.ItalianLeadingZero && !IsLeadingZeroPossible(number.CountryCode);
}
示例9: IsValidNumber
/**
* 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.
*
* @param number the phone number that we want to validate
* @return a boolean that indicates whether the number is of a valid pattern
*/
public bool IsValidNumber(PhoneNumber number)
{
var regionCode = GetRegionCodeForNumber(number);
return IsValidNumberForRegion(number, regionCode);
}
示例10: IsPossibleNumberWithReason
/**
* Check whether a phone number is a possible number. It provides a more lenient check than
* {@link #isValidNumber} in the following sense:
*<ol>
* <li> It only checks the length of phone numbers. In particular, it doesn't check starting
* digits of the number.
* <li> 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.
* <li> 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.
* </ol
* @param number the number that needs to be checked
* @return a ValidationResult object which indicates whether the number is possible
*/
public ValidationResult IsPossibleNumberWithReason(PhoneNumber number)
{
var nationalNumber = GetNationalSignificantNumber(number);
int countryCode = number.CountryCode;
// Note: For Russian Fed and NANPA numbers, we just use the rules from the default region (US or
// Russia) since the getRegionCodeForNumber will not work if the number is possible but not
// valid. This would need to be revisited if the possible number pattern ever differed between
// various regions within those plans.
if (!HasValidCountryCallingCode(countryCode))
return ValidationResult.INVALID_COUNTRY_CODE;
String regionCode = GetRegionCodeForCountryCode(countryCode);
PhoneMetadata metadata = GetMetadataForRegionOrCallingCode(countryCode, regionCode);
PhoneNumberDesc generalNumDesc = metadata.GeneralDesc;
// Handling case of numbers with no metadata.
if (!generalNumDesc.HasNationalNumberPattern)
{
int numberLength = nationalNumber.Length;
if (numberLength < MIN_LENGTH_FOR_NSN)
return ValidationResult.TOO_SHORT;
if (numberLength > MAX_LENGTH_FOR_NSN)
return ValidationResult.TOO_LONG;
return ValidationResult.IS_POSSIBLE;
}
var possibleNumberPattern =
regexCache.GetPatternForRegex(generalNumDesc.PossibleNumberPattern);
return TestNumberLengthAgainstPattern(possibleNumberPattern, nationalNumber);
}
示例11: IsPossibleNumber
/**
* Convenience wrapper around {@link #isPossibleNumberWithReason}. Instead of returning the reason
* for failure, this method returns a boolean value.
* @param number the number that needs to be checked
* @return true if the number is possible
*/
public bool IsPossibleNumber(PhoneNumber number)
{
return IsPossibleNumberWithReason(number) == ValidationResult.IS_POSSIBLE;
}
示例12: IsNumberMatch
/**
* Takes two phone numbers and compares them for equality. This is a convenience wrapper for
* {@link #isNumberMatch(PhoneNumber, PhoneNumber)}. No default region is known.
*
* @param firstNumber first number to compare in proto buffer format.
* @param secondNumber second number to compare. Can contain formatting, and can have country
* calling code specified with + at the start.
* @return NOT_A_NUMBER, NO_MATCH, SHORT_NSN_MATCH, NSN_MATCH, EXACT_MATCH. See
* {@link #isNumberMatch(PhoneNumber, PhoneNumber)} for more details.
*/
public MatchType IsNumberMatch(PhoneNumber firstNumber, String secondNumber)
{
// First see if the second number has an implicit country calling code, by attempting to parse
// it.
try
{
PhoneNumber secondNumberAsProto = Parse(secondNumber, UNKNOWN_REGION);
return IsNumberMatch(firstNumber, secondNumberAsProto);
}
catch (NumberParseException e)
{
if (e.ErrorType == ErrorType.INVALID_COUNTRY_CODE)
{
// The second number has no country calling code. EXACT_MATCH is no longer possible.
// We parse it as if the region was the same as that for the first number, and if
// EXACT_MATCH is returned, we replace this with NSN_MATCH.
String firstNumberRegion = GetRegionCodeForCountryCode(firstNumber.CountryCode);
try
{
if (!firstNumberRegion.Equals(UNKNOWN_REGION))
{
PhoneNumber secondNumberWithFirstNumberRegion = Parse(secondNumber, firstNumberRegion);
MatchType match = IsNumberMatch(firstNumber, secondNumberWithFirstNumberRegion);
if (match == MatchType.EXACT_MATCH)
return MatchType.NSN_MATCH;
return match;
}
else
{
// If the first number didn't have a valid country calling code, then we parse the
// second number without one as well.
var secondNumberProto = new PhoneNumber.Builder();
ParseHelper(secondNumber, null, false, false, secondNumberProto);
return IsNumberMatch(firstNumber, secondNumberProto.Build());
}
}
catch (NumberParseException)
{
// Fall-through to return NOT_A_NUMBER.
}
}
}
// One or more of the phone numbers we are trying to match is not a viable phone number.
return MatchType.NOT_A_NUMBER;
}
示例13: GetRegionCodeForNumber
/**
* Returns the region where a phone number is from. This could be used for geocoding at the region
* level.
*
* @param number the phone number whose origin we want to know
* @return the region where the phone number is from, or null if no region matches this calling
* code
*/
public String GetRegionCodeForNumber(PhoneNumber number)
{
List<String> regions = null;
countryCallingCodeToRegionCodeMap_.TryGetValue(number.CountryCode, out regions);
if (regions == null)
{
// String numberString = getNationalSignificantNumber(number);
// LOGGER.log(Level.WARNING,
// "Missing/invalid country_code (" + countryCode + ") for number " + numberString);
return null;
}
if (regions.Count == 1)
return regions[0];
return GetRegionCodeForNumberFromRegionList(number, regions);
}
示例14: GetNumberType
/**
* Gets the type of a phone number.
*
* @param number the phone number that we want to know the type
* @return the type of the phone number
*/
public PhoneNumberType GetNumberType(PhoneNumber number)
{
var regionCode = GetRegionCodeForNumber(number);
if (!IsValidRegionCode(regionCode) && !REGION_CODE_FOR_NON_GEO_ENTITY.Equals(regionCode))
return PhoneNumberType.UNKNOWN;
var nationalSignificantNumber = GetNationalSignificantNumber(number);
PhoneMetadata metadata = GetMetadataForRegionOrCallingCode(number.CountryCode, regionCode);
return GetNumberTypeHelper(nationalSignificantNumber, metadata);
}
示例15: GetRegionCodeForNumberFromRegionList
private String GetRegionCodeForNumberFromRegionList(PhoneNumber number,
List<String> regionCodes)
{
String nationalNumber = GetNationalSignificantNumber(number);
foreach (var regionCode in regionCodes)
{
// If leadingDigits is present, use this. Otherwise, do full validation.
PhoneMetadata metadata = GetMetadataForRegion(regionCode);
if (metadata.HasLeadingDigits)
{
if (regexCache.GetPatternForRegex(metadata.LeadingDigits)
.MatchBeginning(nationalNumber).Success)
return regionCode;
}
else if (GetNumberTypeHelper(nationalNumber, metadata) != PhoneNumberType.UNKNOWN)
return regionCode;
}
return null;
}