Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Stirng replacement seemingly simple but I couldnt find a simple way

 
Dhiren Joshi
Ranch Hand
Posts: 463
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author
Saloon Keeper
Posts: 4020
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jaime M. Tovar
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Scott Selikoff
author
Saloon Keeper
Posts: 4020
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, much better! Thanks Ernest, that was confusing!
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 463
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author
Saloon Keeper
Posts: 4020
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic