Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formatting Strings

Status
Not open for further replies.

priyaX

Technical User
Jan 12, 2005
10
US
Hi Every1.

Does anyone has a pre 1.5 approach to formatting and displaying text Strings rendered via a JLabel ?

Say like you have a String phone = "123.456.7890"

And I would like it to be displayed as :-

(123)-456-7890

Does any1 care to give me an approach to this ?

Thanks alot.


 
Look at the various methods in the String class - replaceAll(), substring(), indexOf() etc, and the regex package :


Without telling us your precise reqirements, its hard to help, but, for example, this will replace all the "dots" to "dashes" :

String s = "123.456.7890";
s = s.replaceAll(".", "-");

How you put in the brackets depends on where you want them, how differing the data is etc - you may want to look at regex to replace the beinging of the string - "^" to the first dot with brackets.

--------------------------------------------------
Free Database Connection Pooling Software
 
Thanks for your response sedj. Nope , my requirements are pretty basic. Just want 2 know the best way of performing String formatting.

My solution is basically :


String in=getUnformattedPhone();
in=in.replace(".","");
Object out=null;
MaskFormatter mf=new MaskFormatter();
try
{
mf.setMask("(###)-###-####");
JFormattedTextField field=new JFormattedTextField(mf);
field.setText(in);
out=field.getText();
}
catch(ParseException _pe)
{
System.out.println("Exception :"+_pe.getMessage());
}
System.out.println("AFTER = >"+out);


This does what I want but I just wondered whether there was a more elegant way without the need to create a new JFormattedText field


TIA
 
That looks fairly tight to me - I'm sure there is someone out there with a single (if not large) line of regex, buts certainly not me :)

--------------------------------------------------
Free Database Connection Pooling Software
 
here we go:
Code:
String source = "1234567890";
String pattern = "([0-9]{3})([0-9]{3})([0-9]{4})";
System.out.println ("pattern: " + pattern);
String result = source.replaceAll (pattern, "($1)-$2-$3");
System.out.println ("result: " + result);

seeking a job as java-programmer in Berlin:
 
Well done Stefan :)
I knew there must be a regex out there that was better !

--------------------------------------------------
Free Database Connection Pooling Software
 
The mask solution looks more elegant, but if the project isn't using swing at all, I would probably use regex too.

I can recommend sed, which makes heavy use of regexes, to learn about them, and the book 'Helmut Herold, sed and awk, Addisson Wesley' (don't know if an english version was ever printed, have a german original).

Behind shell and java its my most productive tool for special but often needed tasks.

seeking a job as java-programmer in Berlin:
 
sedj ... your moniker could be read as 'sed-for-java'. You are a regex-processing tool.. [bigsmile]

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
LOL, if only I was. regex leaves me in cold sweats :)

--------------------------------------------------
Free Database Connection Pooling Software
 
Hi,

im having a similar problem using the regex string. im trying to convert a '<' and a '>' to &lt; and &gt; respectively so that when im displaying these in my jsp pages they wont be detected as tags.

ive tried a few variations of this code but none of them have worked at all.

it doesnt like the \p as an escape character. so i removed it and did some other variation of it with no joy.
Code:
email.replaceAll("(\p[Punct]{<})", "&lt;");

thanks for any help on this cus it recking me head.
 
Never mind found a thread on the same topic and that did the trick.

Code:
email = email.replaceAll("\"", "&quot;");
email = email.replaceAll("</?", "&lt;";
email = email.replaceAll(">/?", "&gt;";

cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top