• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

phone number formatter in struts2

 
Sandeep Kumar.
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have to display a phone number in (xxx)xxx-xxx format while i'm getting the phone number from the db as a 10 digit number(e.g. - 1324527865)

There are a lot of example on formatting date and numbers but no example on formatting strings(inserting comma,dot etc).

Any help will be much appreciated.



 
Mike Cobelmach
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use static method which convert string from database to your phone number format. More informations are in mail-archive.com.
 
Roberto Hernandez
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a string, the best thing to do is to create a method that uses Java String Manipulation to convert to the format you want. In this case, you'll have to insert a parenthesis ( at the first position, ) at the third position and/or a hyphen - at the 4th position (999) 999-999. It shouldn't be that complicated. Use google and you'll find some examples
 
Sandeep Kumar.
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I could always write a java code for masking and unmasking phone number which is what i am doing for time being.

but i wanted to define some format in ResourceBundle (e.g. item.price={0,number,currency}) so that i don't have to do the conversion by myself every time.

struts uses java.text.MessageFormat class for formatting and i don't know how to use MessageFormat class for string formatting .

still waiting for help.......
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic