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;
}


35 comments :

  1. Can we do this(this works!)
    public String withoutString(String base, String remove) {

    base=base.replace(remove.toUpperCase(),"");
    base=base.replace(remove.toLowerCase(),"");
    return base.replace(remove,"");
    }

    ReplyDelete
    Replies
    1. It won't work for below input scenario
      withoutString("THIS Is a FISH", "iS")

      Delete
    2. yea i knew this solution but the goal for practise is to make a loop. https://codingbat.com/

      Delete
  2. Try this.

    public String withoutString(String base, String remove) {
    String result1 = "";
    String result2 = "";
    String result3 = "";
    String lowerCaseRemove = remove.toLowerCase();
    String upperCaseRemove = remove.toUpperCase();
    if(base.contains(remove)==false && base.contains(lowerCaseRemove)==false && base.contains(upperCaseRemove)==false){
    return base;
    }
    if(base.contains(remove) || base.contains(lowerCaseRemove) || base.contains(upperCaseRemove)){
    result1 = base.replaceAll(remove,"");
    result2 = result1.replaceAll(lowerCaseRemove,"");
    result3 = result2.replaceAll(upperCaseRemove,"");
    }
    return result3;
    }

    ReplyDelete
  3. This works for me - i don't know if it is an elegant solution but it works -


    public String withoutString(String base, String remove) {

    String baseL = base.toLowerCase();
    String subStr;

    int start = 0;
    while(baseL.contains(remove.toLowerCase())){
    start = baseL.indexOf(remove.toLowerCase());

    subStr = base.substring(start, start+remove.length());
    base = base.replace(subStr, "");
    baseL = base.toLowerCase();

    }


    return base;
    }

    ReplyDelete
  4. static String withoutString(String base, String remove) {

    int baseL= base.length();
    int remL = remove.length();
    String result = "";

    for(int i=0; i<baseL; i++){
    if((i+remL-1)<baseL && base.substring(i, i+remL).toLowerCase().equals(remove.toLowerCase()) ){
    base = base.substring(0, i)+base.substring(i+remL, baseL);
    baseL= base.length();
    i--;
    }
    }
    return base;
    }

    ReplyDelete
  5. how about that? :)

    public String withoutString(String base, String remove) {
    String result="";
    int b = base.length();
    int r = remove.length();

    for (int i=0; i<b;i++) {
    if (i<b-r+1 && base.substring(i,i+r).toLowerCase().equals(remove.toLowerCase())) {
    i=i+r-1;
    continue;
    }
    result+=base.substring(i,i+1);
    }
    return result;
    }

    ReplyDelete
  6. Ok when i solved this problem i was like ok this looks easy then i ended up doing this LOL wtf

    public String withoutString(String base, String remove) {
    String newStr = "";
    int len = base.length();
    for(int i = 0; i < len - remove.length() + 1; i++){
    if(base.length()>= remove.length()){
    if(base.substring(0,remove.length()).equalsIgnoreCase(remove))
    base = base.substring(remove.length());
    else{
    if(base.length() == remove.length()){
    if(base.equalsIgnoreCase(remove))
    base = base.substring(remove.length());
    else{
    newStr+= base;
    break;
    }
    }
    else
    while(base.length() > remove.length() &&!base.substring(0,remove.length()).equalsIgnoreCase(remove)){
    newStr+= base.charAt(0) +"";
    base = base.substring(1);
    }
    }
    }
    }

    if(base.length() < remove.length())
    newStr += base;

    return newStr;
    }

    ReplyDelete
  7. Similar to the answer at the top of the page, except that I used the equalsIgnoreCase method to test for equality.

    public String withoutString(String base, String remove) {

    String result = "";

    int bl = base.length();
    int rl = remove.length();

    for(int i = 0; i < bl; i++){
    if(i <= bl-rl){
    String tmp = base.substring(i, i + rl);
    if( tmp.equalsIgnoreCase(remove) ){
    i += rl - 1;
    } else {
    result += base.charAt(i);
    }
    } else {
    result += base.charAt(i);
    }
    }
    return result;
    }

    ReplyDelete
  8. public String withoutString(String base, String remove) {
    int next = 0;
    while ((next = base.toLowerCase().indexOf(remove.toLowerCase())) >= 0) {
    base = base.substring(0, next) + base.substring(next + remove.length());
    }
    return base;
    }

    This is my solution to this problem. I think sometimes writing clean code worth efficiency. Even if we need that much efficiency, I think this code is easier to make faster.

    ReplyDelete
  9. public String withoutString(String base, String remove) {
    //use of Java pattern flag for case insensitive --- (?i)
    String afterRemove = base.replaceAll("(?i)"+remove,"");
    return afterRemove;
    }

    ReplyDelete
  10. public String withoutString(String base, String remove) {

    String x = "";
    String base_new = base.toLowerCase();
    String remove_new = remove.toLowerCase();
    String remove_upper = remove.toUpperCase();

    if(remove.length()==1) {
    for(int i=0;i<base.length();i++) {
    if(base_new.charAt(i) != remove_new.charAt(0)) {
    x = x + base.charAt(i);
    }
    }
    base = x;
    }
    else {

    base = base.replace(remove,"");
    base = base.replace(remove_new,"");
    base = base.replace(remove_upper,"");
    }
    return base;
    }

    ReplyDelete
  11. This might not be the point of the exercise but for anyone trying to solve with regex. (?i) is case-insensitive flag

    public String withoutString(String base, String remove) {
    return base.replaceAll("(?i)"+remove,"");
    }

    ReplyDelete
  12. public String withoutString(String base, String remove) {
    return base = base.replaceAll("(?i)" + remove, "");
    }

    I did it like this

    ReplyDelete
  13. public String withoutString(String base, String remove) {
    int len = remove.length();
    if(len > base.length()){
    return base;
    }
    for( int i = 0 ; i < len ; i++){
    if(base.substring(0,len).equalsIgnoreCase(remove) ){
    return withoutString(base.substring(len), remove) ;
    }else {
    return base.charAt(0) + withoutString(base.substring(1), remove);
    }

    }
    return base;
    }

    ReplyDelete
  14. Just passing by, leaving this here:
    return base.replaceAll(remove,"").replaceAll(remove.toLowerCase(),"").replaceAll(remove.toUpperCase(),"");

    ReplyDelete
  15. public String withoutString(String base, String remove) {
    int i;

    while((i = base.toUpperCase().indexOf(remove.toUpperCase())) != -1){
    base = base.substring(0, i) + base.substring(i+remove.length());
    }
    return base;
    }

    ReplyDelete
  16. Recursive!

    public String withoutString(String base, String remove) {

    if(base.length() == 0) return base;

    if(base.length() >= remove.length()){
    if(base.substring(0,remove.length()).equals(remove)
    || base.substring(0,remove.length()).equals(remove.toUpperCase())
    || base.substring(0,remove.length()).equals(remove.toLowerCase()))
    return withoutString(base.substring(remove.length()), remove);
    }
    return base.charAt(0) + withoutString(base.substring(1), remove);
    }

    ReplyDelete
  17. Shortest way!! it works.

    public String withoutString(String base, String remove) {
    String k = String.format("(?i)%s", remove);

    return (base.replaceAll(k, ""));
    }

    ReplyDelete
  18. public String withoutString(String base, String remove) {
    String ret = "";
    for (int i = 0; i < base.length(); i++)
    {
    if (i + remove.length() <= base.length() && base.substring(i, i + remove.length()).equalsIgnoreCase(remove))
    {
    i += remove.length() - 1;
    }
    else
    {
    ret += base.charAt(i);
    }
    }
    return ret;
    }

    ReplyDelete
  19. public String withoutString(String base, String remove) {
    // eliminate all instances of the "remove" string
    return base
    .replace(remove.toUpperCase(),"")
    .replace(remove.toLowerCase(),"")
    .replace(remove,"");
    }

    ReplyDelete
  20. public String withoutString(String base, String remove) {
    return base
    .replace(remove.toUpperCase(),"")
    .replace(remove.toLowerCase(),"")
    .replace(remove,"");
    }

    ReplyDelete
  21. public String withoutString(String base, String remove) {
    String lowerRemove = remove.toLowerCase();
    String upperRemove = remove.toUpperCase();
    for(int i=0; i<base.length(); i++){
    if(base.substring(i).startsWith(lowerRemove) || base.substring(i).startsWith(upperRemove) || base.substring(i).startsWith(remove)){
    base = base.replace(base.substring(i,i+remove.length()),"");
    }
    }
    return base;
    }

    ReplyDelete
  22. return base.replaceAll("(?i)"+remove,"");

    ReplyDelete
  23. function withoutString(m,n){
    var x = m.replaceAll(n,'');
    console.log(x);
    }
    withoutString("Hello there","llo");

    ReplyDelete
  24. function withoutString(m,n){
    var x = m.replaceAll(n,'');
    console.log(x);
    }
    withoutString("Hello there","llo");
    This is by Using Javascript

    ReplyDelete
  25. Recursive Solution:

    public String withoutString(String base, String remove) {
    if (base.length() < remove.length()) {
    return base;
    }
    if (base.substring(0, remove.length()).toLowerCase().equals(remove.toLowerCase())) {
    return withoutString(base.substring(remove.length()), remove);
    }
    return base.charAt(0) + withoutString(base.substring(1), remove);
    }

    ReplyDelete
  26. public String withoutString(String base, String remove) {
    String res = "";
    String sub = "";
    int i;
    do {
    i = base.toLowerCase(Locale.ROOT).indexOf(remove.toLowerCase(Locale.ROOT));
    if (i != -1) {
    res = base.substring(0, i);
    res = res + sub;
    res = res + base.substring(i + remove.length());
    base = res;
    }

    } while (i != -1);
    return base;
    }

    ReplyDelete
  27. public String withoutString(String base, String remove) {
    return base.replaceAll("(?i)" + remove, "");
    }

    ReplyDelete
  28. public String withoutString(String base, String remove) {
    String str = "";
    str = base.replace(remove, "");
    remove = remove.toUpperCase(Locale.ROOT);
    str = str.replace(remove, "");
    remove = remove.toLowerCase(Locale.ROOT);
    str = str.replace(remove, "");
    return str;
    }

    ReplyDelete
  29. public String withoutString(String base, String remove) {
    String base1 = base.toLowerCase();
    String remove1 = remove.toLowerCase();
    String res = "";
    for(int i = 0; i<base.length(); i++) {
    if(!base1.startsWith(remove1, i)) {
    res = res + base.substring(i, i+1);
    } else {
    i += remove.length()-1;
    }
    }
    return res;
    }

    ReplyDelete

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