PhoneNumberFormatter PhoneNumberFormatter PhoneNumberFormatter PhoneNumberFormatter Class

Definition

Formats phone numbers.

public : sealed class PhoneNumberFormatter : IPhoneNumberFormatterpublic sealed class PhoneNumberFormatter : IPhoneNumberFormatterPublic NotInheritable Class PhoneNumberFormatter Implements IPhoneNumberFormatter// You can use this class in JavaScript.
Attributes
Windows 10 requirements
Device family
Windows 10 Anniversary Edition (introduced v10.0.14393.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v3)

Constructors

PhoneNumberFormatter() PhoneNumberFormatter() PhoneNumberFormatter() PhoneNumberFormatter()

Creates a PhoneNumberFormatter object, using the current default region.

public : PhoneNumberFormatter()public PhoneNumberFormatter()Public Sub New()// You can use this method in JavaScript.

Remarks

Note that you can also create PhoneNumberFormatter objects using PhoneNumberFormatter.TryCreate, which takes a two-letter ISO region name as an argument and attempts to create a PhoneNumberFormatter for the given region.

Methods

Format(PhoneNumberInfo) Format(PhoneNumberInfo) Format(PhoneNumberInfo) Format(PhoneNumberInfo)

Returns a string representation of the provided phone number.

public : PlatForm::String Format(PhoneNumberInfo number)public string Format(PhoneNumberInfo number)Public Function Format(number As PhoneNumberInfo) As string// You can use this method in JavaScript.
Parameters
number
PhoneNumberInfo PhoneNumberInfo PhoneNumberInfo PhoneNumberInfo

The phone number to be formatted. The PhoneNumberInfo specifies not just the subscriber number, but also other relevant information such as country code.

Returns
PlatForm::String string string string

A string containing the formatted phone number, using the default format for the region specified when this PhoneNumberFormatter was created.

See Also

Format(PhoneNumberInfo, PhoneNumberFormat) Format(PhoneNumberInfo, PhoneNumberFormat) Format(PhoneNumberInfo, PhoneNumberFormat) Format(PhoneNumberInfo, PhoneNumberFormat)

Returns a string representing the given phone number, using the given format.

public : PlatForm::String Format(PhoneNumberInfo number, PhoneNumberFormat numberFormat)public string Format(PhoneNumberInfo number, PhoneNumberFormat numberFormat)Public Function Format(number As PhoneNumberInfo, numberFormat As PhoneNumberFormat) As string// You can use this method in JavaScript.
Parameters
number
PhoneNumberInfo PhoneNumberInfo PhoneNumberInfo PhoneNumberInfo

The phone number to be formatted. The PhoneNumberInfo specifies not just the subscriber number, but also other relevant information such as country code.

numberFormat
PhoneNumberFormat PhoneNumberFormat PhoneNumberFormat PhoneNumberFormat

The specific format to be used.

Returns
PlatForm::String string string string

A string containing the phone number, in the specified format.

See Also

FormatPartialString(String) FormatPartialString(String) FormatPartialString(String) FormatPartialString(String)

Returns a string representing the formatted partial phone number given.

Use this method to format phone numbers as they are being entered by a user, to help the user visualize the complete, formatted number.

public : PlatForm::String FormatPartialString(PlatForm::String number)public string FormatPartialString(String number)Public Function FormatPartialString(number As String) As string// You can use this method in JavaScript.
Parameters
number
PlatForm::String String String String

A string representing a partial phone number.

Returns
PlatForm::String string string string

The input string, formatted as a partial phone number.

Remarks

The following table shows the results of repeatedly calling this method as a user enters digits from a phone number, using a formatter set to the "US" region.

Digits passed to FormatPartialStringFormatted output
11
1414
142142
14251 425
14255551 425-555
142555581 425-555-8
142555580801 425-555-8080

The following code snippet demonstrates a TextChanged event handler that updates a TextBlock with the formatted partial number whenever the number typed into the event handler's TextBox changes.


using Windows.Globalization.PhoneNumberFormatting;

PhoneNumberFormatter currentFormatter;

public MainPage()
{
    this.InitializeComponent();

    // Using the current default region
    currentFormatter = new PhoneNumberFormatter();
}

private void gradualInput_TextChanged(object sender, TextChangedEventArgs e)
{
    outBlock.Text = currentFormatter.FormatPartialString(gradualInput.Text);
}

FormatString(String) FormatString(String) FormatString(String) FormatString(String)

Returns the input string, formatted as a phone number.

public : PlatForm::String FormatString(PlatForm::String number)public string FormatString(String number)Public Function FormatString(number As String) As string// You can use this method in JavaScript.
Parameters
number
PlatForm::String String String String

The input string to be formatted.

Returns
PlatForm::String string string string

The input string, formatted as a phone number, using the region specified when this PhoneNumberFormatter was created.

FormatStringWithLeftToRightMarkers(String) FormatStringWithLeftToRightMarkers(String) FormatStringWithLeftToRightMarkers(String) FormatStringWithLeftToRightMarkers(String)

Returns the input string, formatted as a phone number, surrounded by Unicode Left to Right (LTR) markers.

public : PlatForm::String FormatStringWithLeftToRightMarkers(PlatForm::String number)public string FormatStringWithLeftToRightMarkers(String number)Public Function FormatStringWithLeftToRightMarkers(number As String) As string// You can use this method in JavaScript.
Parameters
number
PlatForm::String String String String

The input string to be formatted.

Returns
PlatForm::String string string string

The input string, formatted as a phone number, using the region specified when this PhoneNumberFormatter was created. The string is surrounded by Unicode Left to Right (LTR) markers.

Remarks

Use this method when rendering a phone number for display and the current region settings specify a Right to Left (RTL) language. Phone numbers should still be rendered Left to Right, even when rendered in RTL languages.

GetCountryCodeForRegion(String) GetCountryCodeForRegion(String) GetCountryCodeForRegion(String) GetCountryCodeForRegion(String)

Static method that returns the country code for a given region code.

public : static int GetCountryCodeForRegion(PlatForm::String regionCode)public static int GetCountryCodeForRegion(String regionCode)Public Static Function GetCountryCodeForRegion(regionCode As String) As int// You can use this method in JavaScript.
Parameters
regionCode
PlatForm::String String String String

The two-letter ISO region code.

Returns
int int int int

The telephone country code, as an integer.

GetNationalDirectDialingPrefixForRegion(String, Boolean) GetNationalDirectDialingPrefixForRegion(String, Boolean) GetNationalDirectDialingPrefixForRegion(String, Boolean) GetNationalDirectDialingPrefixForRegion(String, Boolean)

Static method that returns the national direct dialing prefix for the given region.

public : static PlatForm::String GetNationalDirectDialingPrefixForRegion(PlatForm::String regionCode, bool stripNonDigit)public static string GetNationalDirectDialingPrefixForRegion(String regionCode, Boolean stripNonDigit)Public Static Function GetNationalDirectDialingPrefixForRegion(regionCode As String, stripNonDigit As Boolean) As string// You can use this method in JavaScript.
Parameters
regionCode
PlatForm::String String String String

The region code of interest.

stripNonDigit
bool Boolean Boolean Boolean

When true, all non-digit characters are stripped from the returned string.

Returns
PlatForm::String string string string

The national direct dialing prefix for the given region.

TryCreate(String, PhoneNumberFormatter) TryCreate(String, PhoneNumberFormatter) TryCreate(String, PhoneNumberFormatter) TryCreate(String, PhoneNumberFormatter)

Attempts to create a PhoneNumberFormatter for a given region code, and doesn't throw on failure.

public : static void TryCreate(PlatForm::String regionCode, PhoneNumberFormatter phoneNumber)public static void TryCreate(String regionCode, PhoneNumberFormatter phoneNumber)Public Static Function TryCreate(regionCode As String, phoneNumber As PhoneNumberFormatter) As void// You can use this method in JavaScript.
Parameters
regionCode
PlatForm::String String String String

The region code of the new PhoneNumberFormatter.

phoneNumber
PhoneNumberFormatter PhoneNumberFormatter PhoneNumberFormatter PhoneNumberFormatter

On success, a new PhoneNumberFormatter for the requested region code.

On failure, this value is null.

Remarks

The following code snippet demonstrates how to use this method to create a PhoneNumberFormatter for the "BR" region.


    using Windows.Globalization.PhoneNumberFormatting;

    PhoneNumberFormatter currentFormatter = null;

    PhoneNumberFormatter.TryCreate("BR", out currentFormatter);
    if (currentFormatter != null)
    {
        // use the formatter 
    }  
    else
    {
        // formatter could not be created for the given region. 
        // put your fallback logic here.
    }

WrapWithLeftToRightMarkers(String) WrapWithLeftToRightMarkers(String) WrapWithLeftToRightMarkers(String) WrapWithLeftToRightMarkers(String)

Wraps a string with Unicode Left to Right (LTR) markers.

public : static PlatForm::String WrapWithLeftToRightMarkers(PlatForm::String number)public static string WrapWithLeftToRightMarkers(String number)Public Static Function WrapWithLeftToRightMarkers(number As String) As string// You can use this method in JavaScript.
Parameters
number
PlatForm::String String String String

The string to be surrounded with LTR markers.

Returns
PlatForm::String string string string

The input string, surrounded with LTR markers.