• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String.replace - a different problem

 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??
 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ; - )


prints:
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Peter,

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.

Thanks
 
Peter Taucher
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't like censorship (generally speaking), but of course - we may have different opinions and you're the guy in charge here.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not censorship as such, it's more we like users to find answers themselves. That helps them best. Read http://faq.javaranch.com/java/LetThemDoTheirOwnHomework and http://faq.javaranch.com/java/DontBeACodeMill 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.
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
like:
A->###->APPLE

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.

 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
The happiness of your life depends upon the quality of your thoughts -Marcus Aurelius ... think about this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic