Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Format a string as a telephone number [closed]

Tags:

java

I have an unformatted telephone number string ("5551234567"). I would like to format this string for display purposes in the ui. The number should look like this after format:

(555) 123-4567

Sigh.....

I was trying to find a generic way that would take into account both US and international phone numbers. I came accross the MaskFormatter in the JDK but discovered that there is a bug in the Javadoc. Which lead me to asking my question here. I was hoping for a solution where I could input a mask then the actual string. The output would be formatted accordingly for international/US numbers or if the wrong number of characters were specified, some default character would be displayed in place of the missing characters........

http://www.java.net/node/660119

like image 871
Justin Kredible Avatar asked Nov 30 '22 07:11

Justin Kredible


1 Answers

String.format("(%s) %s-%s", number.substring(0, 3), number.substring(3, 6), 
          number.substring(6, 10));
like image 72
John B Avatar answered Dec 09 '22 20:12

John B