8

I'm doing some beginner's coding practice and I ran into this problem:Given two strings, base and remove, return a version of the base string where all instances of the remove string have been removed. (not case sensitive).

This is what I have so far, but it's not working at all.

public String withoutString(String base, String remove) {

 for (int i=0; i<base.length()-remove.length(); i++){
  if (base.substring(i, i+remove.length()).equals(remove)){
  base = base.substring(i, base.indexOf("remove")-1) + base.substring(base.indexOf("remove"), base.length()-remove.length());
    }
  }
  return base;
}

I've yet to deal with the case sensitive part to make it more visible for myself. I'm also not sure why I can't use base.replaceAll("remove",""); Any help is appreciated.

EDIT*: I made a rookie mistake and the replaceAll still works. Additionally, how could I have done this using loops and conditionals? Would it have been messy like what I had previously?

xheyhenry
  • 1,049
  • 2
  • 13
  • 25
  • possible duplicate of [How to replace case-insensitive literal substrings in Java](http://stackoverflow.com/questions/5054995/how-to-replace-case-insensitive-literal-substrings-in-java) – Aurand Sep 19 '13 at 23:40

4 Answers4

10

You can use

String result = base.replaceAll(remove,"");

With quotes as you where trying it is actually trying to remove the string "remove".

To deal with the case insenstive you can use the regex flag for ignore case (?i) in the front so then you can call

String result = base.replaceAll("(?i)" + remove,"");

This does mean that the String remove, is now a regular expression now so special characters may have undesired results. For example if your remove string was ., you would end up with every character removed. If you don't want it as a regex then use

String result =  Pattern.compile(remove, Pattern.LITERAL).matcher(base).replaceAll("")

Which can also include the case insensitive flag like so as they are a bitmask, see Pattern for more

Pattern.LITERAL | Pattern.CASE_INSENSITIVE

EDIT

To do it using your loop, just do this loop

for (int i=0; i <= base.length()-remove.length(); i++)
{
    if (base.substring(i, i+remove.length()).equals(remove))
    {  
        base = base.substring(0, i) + base.substring(i + remove.length() , base.length());
        i--;
    }
}
Java Devil
  • 10,629
  • 7
  • 33
  • 48
  • Wow I can't believe I missed that. This works for the example problem I had. Additionally, how could I have done this using loops and conditionals? Would it have been messy like what I had previously? This problem was marked as difficult and I don't think it was intended to be done with such a simple statement. Thanks for the reply as well. – xheyhenry Sep 19 '13 at 23:49
  • @xheyhenry See my answer. You messed up a value reference and the usage of a fixed string. – dognose Sep 19 '13 at 23:51
  • Oops, wrong paste. You should compile with the case-insensitive and literal pattern and matcher objects. What if . was the string to be removed? – nanofarad Sep 19 '13 at 23:53
  • @hexafraction Yes you should, my intention of the comment I made in the post was to raise this issue. – Java Devil Sep 19 '13 at 23:59
  • @JavaDevil Where do you mention the literal constant for matching mode? – nanofarad Sep 20 '13 at 00:01
  • @JavaDevil I tried what you edited, and it works for most cases except when there are multiple occurrences of the same string. For example, (Hexxxxllo, xx) only removed one instance of xx since it runs once if the condition is met. To account for this, would you do a while loop? – xheyhenry Sep 20 '13 at 00:05
  • @hexafraction I don't mention it explicitly and I never said I did, I said I raised the Issue that the string would be treated as a regex and as such that certain characters have special meaning – Java Devil Sep 20 '13 at 00:06
  • @xheyhenry See my edit, add 'i--;' after the replacement so that the for loop will repeat the replacement at the same place, except this needs an adjustment if its at the end of the base string.. looking at it now – Java Devil Sep 20 '13 at 00:08
  • @xheyhenry the ending was due to loop not going far enough, needs to be changed to `<=` – Java Devil Sep 20 '13 at 00:28
1

indexOf("remove") means, you are searching for the (fixed) STRING remove, not for the value of the String named remove - which is most likey not, what you want to do. Same applies for your replaceAll("remove") attempt.

remove the " so you are using the VALUE of the String named remove, not the fixed string "remove"

Example:

String remove = "test";
System.out.println(remove) // will print: test
System.out.println("remove") // will print: remove
dognose
  • 20,360
  • 9
  • 61
  • 107
0

You should use (?i) flag or :

base = Pattern.compile(remove, Pattern.CASE_INSENSITIVE).matcher(base).replaceAll("");
Melih Altıntaş
  • 2,495
  • 1
  • 22
  • 35
0

Try this :

if(base.indexOf(remove) != -1){
base.replaceAll(remove,"");
}
Crickcoder
  • 2,135
  • 4
  • 22
  • 36