| Author |
Stirng replacement seemingly simple but I couldnt find a simple way
|
Dhiren Joshi
Ranch Hand
Joined: Dec 09, 2003
Posts: 463
|
|
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. Thanks Dhiren [ EJFH: Fixed the "&" characters in the original message. ] [ November 03, 2005: Message edited by: Ernest Friedman-Hill ]
|
 |
Scott Selikoff
Saloon Keeper
Joined: Oct 23, 2005
Posts: 3652
|
|
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 ]
|
My Blog: Down Home Country Coding with Scott Selikoff
|
 |
Jaime M. Tovar
Ranch Hand
Joined: Mar 28, 2005
Posts: 133
|
|
|
I think the str.replaceAll(" "," "); instruction will ever return the same string. And i think a web page will not remove spaces as long as they are between two significant characters.
|
She will remember your heart when men are fairy tales in books written by rabbits.<br /> As long as there is duct tape... there is also hope.
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24041
|
|
Scott and Jim -- I've fixed the original poster's message: he was already using " " as you can see, but it wasn't displaying right in the forum software.
|
[Jess in Action][AskingGoodQuestions]
|
 |
Scott Selikoff
Saloon Keeper
Joined: Oct 23, 2005
Posts: 3652
|
|
|
Ah, much better! Thanks Ernest, that was confusing!
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24041
|
|
Dhiren, 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.
|
 |
Dhiren Joshi
Ranch Hand
Joined: Dec 09, 2003
Posts: 463
|
|
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
|
 |
Scott Selikoff
Saloon Keeper
Joined: Oct 23, 2005
Posts: 3652
|
|
|
This has been a great post in futilism in that spaces, tabs, and non-breaking spaces all display the same! Someone pass me more coffee...
|
 |
Tom Blough
Ranch Hand
Joined: Jul 31, 2003
Posts: 263
|
|
Better yet, use It will replace all occurances of contiguous whitespace (tabs, newlines, spaces, carriage returns, form feeds) in the string with a single non-breaking space. Cheers, [ November 03, 2005: Message edited by: Tom Blough ]
|
Tom Blough<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>Cum catapultae proscriptae erunt tum soli proscripti catapultas habebunt.<hr></blockquote>
|
 |
 |
|
|
subject: Stirng replacement seemingly simple but I couldnt find a simple way
|
|
|