public static bool IsNationalPrefixPresentIfRequired(PhoneNumber number, PhoneNumberUtil util) { // First, check how we deduced the country code. If it was written in international format, then // the national prefix is not required. if (number.CountryCodeSource != PhoneNumber.Types.CountryCodeSource.FROM_DEFAULT_COUNTRY) { return(true); } var phoneNumberRegion = util.GetRegionCodeForCountryCode(number.CountryCode); var metadata = util.GetMetadataForRegion(phoneNumberRegion); if (metadata == null) { return(true); } // Check if a national prefix should be present when formatting this number. var nationalNumber = util.GetNationalSignificantNumber(number); var formatRule = util.ChooseFormattingPatternForNumber(metadata.NumberFormatList, nationalNumber); // To do this, we check that a national prefix formatting rule was present and that it wasn't // just the first-group symbol ($1) with punctuation. if (formatRule != null && formatRule.NationalPrefixFormattingRule.Length > 0) { if (formatRule.NationalPrefixOptionalWhenFormatting) { // The national-prefix is optional in these cases, so we don't need to check if it was // present. return(true); } // Remove the first-group symbol. var candidateNationalPrefixRule = formatRule.NationalPrefixFormattingRule; // We assume that the first-group symbol will never be _before_ the national prefix. candidateNationalPrefixRule = candidateNationalPrefixRule.Substring(0, candidateNationalPrefixRule.IndexOf("${1}", StringComparison.Ordinal)); candidateNationalPrefixRule = PhoneNumberUtil.NormalizeDigitsOnly(candidateNationalPrefixRule); if (candidateNationalPrefixRule.Length == 0) { // National Prefix not needed for this number. return(true); } // Normalize the remainder. var rawInputCopy = PhoneNumberUtil.NormalizeDigitsOnly(number.RawInput); var rawInput = new StringBuilder(rawInputCopy); // Check if we found a national prefix and/or carrier code at the start of the raw input, and // return the result. return(util.MaybeStripNationalPrefixAndCarrierCode(rawInput, metadata, null)); } return(true); }