0

Hello I have a string and when I try to use replace method in for loop it doesn't work

String phrase="hello friend";
String[] wordds=phrase.split(" ");
String newPhrase="sup friendhello weirdo";
for (int g=0;g<2;g++)
{          
   finalPhrase+=newPhrase.replace(wordds[g],"");
}   
System.out.println(finalPhrase);

It prints out sup hello weirdo and I expect it to print sup weirdo.

What am I doing wrong?

John Smith
  • 844
  • 8
  • 26

5 Answers5

5

Let's debug it together.

wordds = ["hello", "friend"].

newPhrase = "sup friendhello weirdo".

Then you're running on some g from 0 to 1 (Should be from 0 to wordds.length.

newPhrase.replace(wordds[g],""); will indeed replace as you want, but when you debug your program, you'll notice that you are using += instead of:

newPhrase=newPhrase.replace(wordds[g],"");

Tip for life: use the debugger, it's there to help you.

Maroun
  • 94,125
  • 30
  • 188
  • 241
4

Try this:

String phrase = "hello friend";
String[] wordds = phrase.split(" ");
String newPhrase = "sup friendhello weirdo";
for (int g = 0; g < 2 ; g++) {          
  newPhrase = newPhrase.replace(wordds[g], "");
}   
System.out.println(newPhrase);

===================================================

updated

few things that you need to correct

  1. you need to remove concat oprator (+), when you try to replace particular word in a sentence. Just assign it after replacing

  2. for each time you enter the loop you are taking the initial declared string, instead you need to use the string which is getting updated each time

upog
  • 4,965
  • 8
  • 42
  • 81
1

what are you doing, is keep append the replaced Phrase to another one

newPhrase = newPhrase.replace(wordds[g],"");
Unix von Bash
  • 735
  • 5
  • 20
1

Apart from the suggestions of an immediate fix, you can also consider a regex-based solution, with no loops:

String phrase="hello friend";
String regex=phrase.replace(' ', '|');
String newPhrase="sup friendhello weirdo";
String finalPhrase=newPhrase.replaceAll(regex,"");
System.out.println(finalPhrase);

or, more succinctly:

System.out.println("sup friendhello weirdo"
                   .replaceAll("hello friend".replace(' ','|'), 
                               ""));
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0

This should do the trick:

String phrase="hello friend";
String[] wordds=phrase.split(" ");
String newPhrase="sup friendhello weirdo";
String finalPhrase=newPhrase;
for (int g=0;g<wordds.length;g++)
{          
   finalPhrase=finalPhrase.replace(wordds[g],"");
}   
System.out.println(finalPhrase);

First you assign finalPhrase to your newPhrase. Then you iterate over all split words (i've changed your magic constant 2 into number of split words wordds.length. Every word will be replaced in the finalPhrase string. The resulting string looks like sup weirdo (it has two spaces between words).

You can clean extra spaces using the answer from here:

System.out.println(finalPhrase.trim().replaceAll(" +", " "));
Community
  • 1
  • 1
nio
  • 5,141
  • 2
  • 24
  • 35