Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Converting the String to Phone format" Watch "Converting the String to Phone format" New topic
Author

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..

Thanks


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
Greenhorn

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

Joined: Sep 28, 2004
Posts: 18546
    
  40

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...



Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Converting the String to Phone format
 
Similar Threads
fmt error phone number
Is a long String to form.append bad for phone?
Phone number validation code
Formatting in JTable?
formatting phone number using NumberFormat?