Java > String-3 > withoutString (CodingBat Solution)

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). You may assume that the remove string is 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"


Solution:

public String withoutString(String base, String remove) {
  int blen = base.length();
  int rlen = remove.length();
  String lowbase = base.toLowerCase();
  String lowrem = remove.toLowerCase();
  String fin = "";

  for (int i = 0; i < blen; i++) {
    if (i <= blen - rlen) {
      String tmp = lowbase.substring(i,i+rlen);
      if (!tmp.equals(lowrem))
        fin += base.substring(i,i+1);
      else {
        i += rlen-1;
      }
    }
    else {
      String tmp2 = lowbase.substring(i,i+1);
      if (!tmp2.equals(lowrem))
        fin += base.substring(i,i+1);
    }
  }
  
  return fin;
}


No comments :

Post a Comment

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact