• Post Reply Bookmark Topic Watch Topic
  • New Topic

need to encode a string  RSS feed

 
Thomas Rochon
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there !
I want to encode a String so that it turns
a space into %20 , a dot into %2E , a colon
into %3A and a slash into %2f .
I tried out URLEncode from java.net, but
it turned the space into "+" and ignored
the dot.
Do you have any idea how I can solve that
problem ?
Thanx !
- Thomas -
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How big of a String are you talking about, and where is it coming/going to?
Personally, I'd turn it into a StringBuffer, cycle through the characters based on the length, and replace the characters as they're discovered.
Honestly don't think this is really an I/O related question, so I'll move this to a more appropriate forum if you don't get better feedback.
 
Thomas Rochon
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx for your reply, Jason !
Sorry for posting this topic possibly into
the wrong forum, but I wasn't sure into which
forum this topic would fit best.
The String I want to encode isn't that big.
It will be an URL and/or a small folder name
(maybe that sounds a bit strange to you but the
developer of an instand messenger put the description of web bookmarks into a xml file,
using that encoding).
I want to write a small Application that can
delete, change and enhance the entries in that
xml file. That's what it's all about
Best regards,
- Thomas -
 
Thomas Rochon
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
O.k. - I'll do like you told me ...
I'll put the String into a StringBuffer,
searching for the characters I want to encode by 'charAt(int index)' and replace the located characters with 'replace(int start, int end, String str)'.
Does that sound like an acceptable solution ?
Thanx for your help, Jason !
Best regards,
- Thomas -
btw: Do you know what kind of encoding that developer is using (in reference to my first posting)? Is it a java supportet encoding style?
[ March 06, 2003: Message edited by: Thomas Rochon ]
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I created two classes (both in one file, for demonstration purposes only). The code is the following:

Both print out "Thi%4 i%4 a te%4t"
Just wanted to show there are many different ways to do this. Personally, I think the TestReplaceA is better. You might even rewrite that method as switchChar( String full , String old , String new ), and have that within a class you could reuse.
 
Thomas Rochon
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much, Jason !
Even if this was the wrong forum to post
my question to, it was very much helpfull
to me !
At least I got my problem solved
Thanx again !
- Thomas -
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, we aim to please (within reason )
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason, what's that "encode" argument, anyway?
Using indexOf() and StringBuffer's replace() seems inefficient here. Since there are four different characters to look for, you end up examining the whole string four times. More annoyingly, each time replace() is called it will result in an array copy of the latter part of the StringBuffer contents, as letters are shifted a few chars to the right to make space for the new chars. And a new String object is created. This is annoying because it happens each time a replacement is made. Much better to avoid copying the later bytes and creating a new String until all the replacements have been made. The replaceAll() method seems to be an improvement:

This looks pretty simple, but each replaceAll() is hiding a number of object creations in its implementation. It's easy to use (as long as you know when you need escape chars in a regex pattern) but still not overly efficient.
Here's my preferred recommendation - a straightforward way to scan each char in the input string exactly once, and create only two objects during the entire process:
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The encode argument was meant to be the third part of replace(), but forgot to use it. Heck, I was going from the top of my head.
I hardly use, if ever, switch statements, so unfortunately I tend to forget about them. I was trying to think of a way to come up with a method that you could reuse, that was independent of the String and characters.
Jim's solution is quite nice and efficient, and I should have learned by now all the background stuff that goes on, but you sometimes forget those underlying details. That's why he's a sheriff and I'm still serving up cheap swill
 
Thomas Rochon
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx Masters !
Thank you for spending your valuable time to help an unworthy greenhorn

Thanx a lot !
- Thomas -
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!