How to format a phone numbers with libphonenumber in International format.

前端 未结 2 602
面向向阳花
面向向阳花 2021-02-13 21:15

In the documentation provided by libphonenumber on Github, there is a demo, which converts any provided number into International Format, e.g +4915213116250 is conv

2条回答
  •  萌比男神i
    2021-02-13 22:01

    @danyolgiax almost had it. Here's what you do:

    var phoneUtil = PhoneNumberUtil.GetInstance();
    var numberProto = phoneUtil.Parse("+4915213116250", "ZZ"); //why ZZ means international i don't know
    var formattedPhone = phoneUtil.Format(numberProto, PhoneNumberFormat.INTERNATIONAL);
    

    Remember to use phoneUtil.IsValidNumber if you aren't certain that the phone number is valid. Also be aware that phoneUtil.Parse may throw.

提交回复
热议问题