ttacon/libphonenumber

怎么获取城市区号

fengweiqiang opened this issue · 3 comments

怎么获取城市区号?能不能举个例子?

Good question! I'll update the README too to add retrieving the city code as an example.

The following should work:

num, err := libphonenumber.Parse("1234567890", "US")
if err != nil {
        // Handle error appropriately.
}

natSigNumber := libphonenumber.GetNationalSignificantNumber(num)
geoCodeLength := libphonenumber.GetLengthOfGeographicalAreaCode(num)
areaCode := ""
if geoCodeLength > 0 {
        areaCode = natSigNumber[0:geoCodeLength]
}
fmt.Println(areaCode)  

I've also update the README with this example.