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). You may assume that the remove string is of length 1 or more. Remove only non-overlapping instances, so with "xxx" removing "xx" leaves "x".
withoutString("Hello there", "llo") → "He there"
withoutString("Hello there", "e") → "Hllo thr"
withoutString("Hello there", "x") → "Hello there"
Why can't I use this code:
public String withoutString(String base, String remove)
{
base.replace(remove, "");
return base;
}
base.replace
doesn't change the original String
instance, since String
is an immutable class. Therefore, you must return the output of replace
, which is a new String
.
public String withoutString(String base, String remove)
{
return base.replace(remove,"");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With