Java: formatting strings as phone numbers
•
Java
I have an unformatted phone number string ("5551234567") I want to format this string in the UI for display purposes After formatting, the numbers should be as follows:
(555)123-4567
Sigh
I tried to find a common way to consider American and international telephone numbers I encountered maskformatter in JDK, but found an error in Javadoc This leads me to ask my questions here I want a solution where I can enter a mask and then enter the actual string For international / US numbers, the output will be formatted accordingly, or if the wrong number of characters is specified, some default characters will be displayed instead of the missing characters
http://www.java.net/node/660119
Solution
String.format("(%s) %s-%s",number.substring(0,3),number.substring(3,6),number.substring(6,10));
String.format("(%s) %s-%s",number.substring(0,3),number.substring(3,6),number.substring(6,10));
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码