-2
String string = "yesnoyesnoyesnoyesno";
String substring = "no";

How do I remove every occurrence of the substring from the string so it ends up being "yesyesyesyes"?

ClarkJ1
  • 7
  • 1
  • 1

3 Answers3

4

If you are using Java 8+, then you could try splitting the input on "no" and then joining the resulting array together into a string:

String string = "yesnoyesnoyesnoyesno";
string = String.join("", string.split("no"));
System.out.println(string);

This prints:

yesyesyesyes
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

This doesn't use the replace(), so technically it meets the brief:

String str = string.replaceAll("no", "");
Bohemian
  • 412,405
  • 93
  • 575
  • 722
0
    String input = "yesnoyesnoyesno";   
    String output="";
    for(String token : input.split("no") )
        output = output.concat(token);      
    System.out.println(output);  

It prints:

yesyesyesyes
Zahid Khan
  • 2,130
  • 2
  • 18
  • 31