0

I need to change several parts of a single String where the specified parts have some Characters in common; for example:

worker => working

work => worked

note :: "=>" means "changes to"

Using codes such as below put out plenty of bugs:

String txt = "work like a real worker";
String txt2 = txt.replace("worker", "working").replace("work", "worked");

As I tested the codes above, they output "worked like a real workeding", however I need to get "worked like a real working".

Community
  • 1
  • 1
elyar abad
  • 771
  • 1
  • 8
  • 27

4 Answers4

3

If you use replaceAll rather than replace, you can use a regular expression. A regular expression will allow you to use \b to denote word boundaries. Doing it this way, you will only replace whole words rather than sections of words like you are doing at the moment.

String txt2 = txt.replaceAll("\\bworker\\b", "working").replaceAll("\\bwork\\b", "worked");
Michael
  • 41,989
  • 11
  • 82
  • 128
  • It's not working, my friend @Michael ! I need it work simultaneously. Another example: String txt = "D D#"; String txt2 = txt.replace("D", "E").replace("D#", "F"); As I tried your solution in the case above, when replacing for the second time, the Regex engine finds no D# to replace it with F (It finds E# instead!). – elyar abad Jul 17 '19 at 13:32
  • @elyarabad Your requirements are not clear, my friend! In one case, you want to do one thing, in another case you want to do another thing. – Michael Jul 17 '19 at 13:34
  • So would you help with the another case, Please? – elyar abad Jul 17 '19 at 13:41
  • @elyarabad I can't help unless you clarify your requirements. – Michael Jul 17 '19 at 13:42
  • OK, I'm working on a guitar chord organizer and I need to write a function in order to change the scales of a specific tab. Changing the whole chords from scale A into scale D#, for instance. So several Strings would be in hand as the scales are: "Ab", "A", "A#", "Bb", and so on .... when changing the scale from A into A#, as an example, the A chord will turn to A# and all the A# to B. The problem is here: when we replace the "A" with "A#", and then we replace the "A#" with "B", it will return: "A#" and "B#" while I need to get "A#" and "B". – elyar abad Jul 17 '19 at 14:03
  • 1
    @elyarabad Then this cannot be achieved by multiple sequential replaces. You will need to iterate over each character the string in one pass, identify chords, and replace them. – Michael Jul 17 '19 at 14:06
2

If you want to replace whole words, have a look at Michael's answer. If you want to replace some words that may be contained in other words without replacing the target-words of your previous replacement again, you can try adding an intermediate step like this:

String txt = "work like a real worker in the workplace";
String txt2 = txt.replace("worker", "{$1}").replace("work", "{$2}");
String result = txt2.replace("{$1}", "working").replace("{$2}", "worked");

prints

worked like a real working in the workedplace

Michael A. Schaffrath
  • 1,992
  • 1
  • 14
  • 23
1

The problem is, that it first replaces "worker" with "working", which is correct, but after that it replaces the "work" in "working" with "worked", which results in "workeding".

As you see the "work"-replacer replaces the second work too, which it shouldn't, because it doesn't stand alone.

To avoid that, simply put a space behind the work. With the space, it will only replace a work which stands alone without anything standing behind it (because there is a space seperating it from the next word).

It will also replace the space, so make sure to put a space behind the worked too.

It should look like this:

String txt = "work like a real worker";
String txt2 = txt.replace("worker", "working").replace("work ", "worked ");
Fabian M.
  • 37
  • 6
-1

Use Regex

String txt = "work like a real worker";
String txt2 =  txt.replaceAll("\\worker\\b", "working").replaceAll("\\work\\b", "worked");
Bellerofont
  • 1,081
  • 18
  • 17
  • 16