I have this String "Some ValueTelephone 1-zzz-ccc" I need to display it directly on a webPage as is and since in webpage the spaces getting removed so I tried to add a string called for the spaces It would seem simple by simply replacing
String str= new String("Some ValueTelephone 1-zzz-ccc"); str.replaceAll(" "," "); But oddly Java doesnt recognize the characters between Some Value and Telephone as space,so infact what happens is this when I run that command
"Some ValueTelephone 1-zzz-ccc"
So I tried to replace a char since String replacement was searching for a full length of that string but a char should not. and then run the command of replaceAll.. so I tried str.replace(' ','&'); str.replaceAll("&"," "); And oddly nothing changed. The output is same as running replaceAll as before.
I cant run a StringTokenizer as I would lose count of the spacing. Looking for a faster way then count every single spacing. Can anyone suggest a way to do this string replacement. I thought it would be a very simple code.
[ EJFH: Fixed the "&" characters in the original message. ] [ November 03, 2005: Message edited by: Ernest Friedman-Hill ]
I'm going to go out on a limb and say you have something else going on here. I don't see why the spaces should have been removed in the first place. Perhaps a longer code snippet would help.
If you get desperate, though, you could use the HTML non-breaking space character: & nbsp; (without the space between & and n)
On a side note string replacements can also be done using Regular expressions. I find the MessageFormat API good for replacement of strings with variables as well. [ November 03, 2005: Message edited by: Scott Selikoff ]
In editing your message, I note that the "spaces" you typed in betweenn "Value" and "Telephone" aren't spaces at all, but tab characters. Your "replaceAll()" call will only replace space (Hex 0x20), but not tabs (Hex 0x09).
But the deeper issue is that web browsers won't display tabs in normal text -- they'll be rendered as a single space. If you're saying you want tabular data to line up properly, then you're going to have to use an HTML table, or a <PRE> block.
Joined: Dec 09, 2003
Thanks every one for the quick responses. Yes I fixed it now. What seemed to be in the DB is really tabs and so my code is not able to replace it. Oddly tabs doesnt display appropriately in the browser .The chars get truncated. Thanks Dhiren