Periastron
Periastron

Reputation: 663

How to handle case-insensitive string replacement without using Regex in Java

This is a problem from the CodingBat website. I am pasting the problem first and discussing my efforts after that:

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"

This is what I wrote so far:

public String withoutString(String base, String remove) {

   int len_b=base.length();
   int len_r = remove.length();
   String result="";

   if(len_b<1 || len_r<1)
   return "";

   for (int i =0;i<=len_b-len_r;i++)
   {
      if(base.substring(i,i+len_r).equals(remove))
      {
        i=i+len_r-1;
      }

      else
      { 
        result=result+base.substring(i,i+1);
      }  
   } 

   if(!(base.substring(len_b-len_r+1, len_b).equals(remove)))
   result=result+base.substring(len_b-len_r+1, len_b);

return result;
}

This passes all the test cases except for the ones where the removal of the string should be case-insensitive.

For example: withoutString("This is a FISH", "IS") → "Th a FH"

My code gives me "This is a FH" as I haven't handled case sensitivity in my code. I know that with Regex this could be done in one line. I am more interested in knowing if there is a way to handle these kinds of test cases in my present code. Also, please let me know if my code could be made more efficient/elegant.

Upvotes: 4

Views: 4224

Answers (5)

Gsquare
Gsquare

Reputation: 700

public String withoutString(String base, String remove) {
      String b=base.toLowerCase();
      String r=remove.toLowerCase();
      if(b.length()<r.length()) return base;
      if(b.contains(r)) b=b.replaceAll(r,"");
      String temp="";
      int j=0;
      for(int i=0;i<base.length();i++)
        if(j<b.length()){
          if(base.substring(i,i+1).equalsIgnoreCase(b.substring(j,j+1))){
            temp+=base.substring(i,i+1);
            j++;
          }
        }  
      return temp;
    }

Upvotes: 0

Petar Ivanov
Petar Ivanov

Reputation: 1

I did it without any looping :) I suppose it is not the best answer, but it works though

public String withoutString(String base, String remove) {
    String lastString = base.replace(remove, "");
    remove = remove.toLowerCase();
    String veryLastString = lastString.replace(remove, "");
    remove = remove.toUpperCase();
    String veryVeryLastString = veryLastString.replace(remove, "");
    return veryVeryLastString;
}

Upvotes: 0

SharanTheja
SharanTheja

Reputation: 5

public String withoutString(String base, String remove) 
{
    String str=base;
    String str1=remove;
    String str3=str;

    int k=str1.length();

    for(int i=0;i<(str.length()-k+1);i++)
    {
        if(str1.equalsIgnoreCase(str.substring(i, i+k)))
        {
            String str4=str.substring(i, i+k);
            str3=str3.replaceFirst(str4,"" );

        }
    }
    return str3;
}

Upvotes: 0

Skip Head
Skip Head

Reputation: 7760

String has an equalsIgnoreCase(String s) method.

Upvotes: 6

Zava
Zava

Reputation: 763

you can change this statement base.substring(i,i+len_r).equals(remove) to base.substring(i,i+len_r).equalsIgnoreCase(remove) using equalsIgnoreCase method.

hope helpful.

Upvotes: 1

Related Questions