my dog learned polymorphism
The moose likes Java in General and the fly likes String.replace - a different problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "String.replace - a different problem" Watch "String.replace - a different problem" New topic

String.replace - a different problem

Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

I have a parent String - say "ABCHASOIOBBASJSKAKSLK" (something like that)
I get input from the user as what replacement must be made for what substring of the parent String.
The user might say 'replace BALL for B' and 'replace APPLE for A' and 'replace IOP for CHA'..... in the parent.
The problem happens when I do serial replacements.That is, when I'm done with my first replacement and proceed over to the second, replace causes all 'A's (including 'A's in the previously replaced 'BALL' also)to be replaced with 'APPLE'..and so on..but what I want to do is just do a replace for whatever is in the parent String. Thats it.
I hope you guys get me.. 'B' in the parent String must be replaced with 'BALL' and 'A's in the parent String alone must be replaced with 'APPLE', 'A' in 'BALL' must remain as such.
Any ideas??

Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
For my simple mind the natural approach would be:
- Remember index
- Loop until no replacement keys can be found
- In each loop check all available key positions, replace the smallest with the corresponding value

So all keys should be replaced and the index marker should increase so that already replaced values aren't processed again. Okay, sounds funny, looks funny ... but seems to work ; - )


Censorship is the younger of two shameful sisters, the older one bears the name inquisition.
-- Johann Nepomuk Nestroy
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955


I deleted your code. We don't like to simply hand out answers here, but instead try to help folks figure out how to code it themselves.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
I don't like censorship (generally speaking), but of course - we may have different opinions and you're the guy in charge here.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20276

It's not censorship as such, it's more we like users to find answers themselves. That helps them best. Read and for more information.
Although once in a while, the border of what is helping / hinting and what is actually providing full code is not clear. Sometimes an example shows the exact solution.

How To Ask Questions How To Answer Questions
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

Peter, thanks for your reply. I too had almost the same thing in mind - noting down indexes and replacing....but thought there actually must be something out there, that I dont know/I haven't come across..
Sudipta Laha
Ranch Hand

Joined: Aug 23, 2010
Posts: 49
Hi Vinoth,

I was just thinking to implement a different approach for this.

We can implement a unique number or combination of spl char for each replacement of letter and the again replace that back with the original String(required replacement).

There will be certain limitation also:
As the input may be anything, so we need to have the special value for each replacement to be unique and it should not be present in the string.

If you are sure of the input, then this approach may be taken.

Sudipta Kumar Laha
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

Sudipta, thanks for your reply. Actually, I'm not sure of the input and as you say some problem might pop up when the string which we use to replace '###' might already be present in the parent string..
I agree. Here's the link:
subject: String.replace - a different problem
It's not a secret anymore!