Two Laptop Bag
The moose likes Java in General and the fly likes Converting the String to Phone format Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Converting the String to Phone format" Watch "Converting the String to Phone format" New topic

Converting the String to Phone format

Bhavik Patel
Ranch Hand

Joined: Jul 12, 2004
Posts: 155
i have a String 1234567890 and i want it in 123-456-7890 format.. is there anyway i can use the formatter or any other class to achieve this and convert it to phone number format..


SCJP 1.4<br />SCWCD 1.4(91%)<br />Working on SCJD -Bodgitt & Scrapper Constructions...<br /> <br />"It takes 43 muscles to frown & 17 to smile but it doen't take any to just sit there with a dumb look on your face .. Keep Smiling "
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I'd probably write a one line conversion method using substring and concatenation. Or use a generalized string rearranger from my own library. Let's see if somebody chimes in with a way to do it with Formatter.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
dilip upadhyay

Joined: Sep 12, 2006
Posts: 1
u can use replace method of string class
Henry Wong

Joined: Sep 28, 2004
Posts: 20164

Originally posted by dilip upadhyay:
u can use replace method of string class

Using the regex engine to do this may be an overkill, but let's see...


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
I agree. Here's the link:
subject: Converting the String to Phone format
It's not a secret anymore!