简体   繁体   English

如何在不使用Java中的Regex的情况下处理不区分大小写的字符串替换

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

This is a problem from the CodingBat website. 这是CodingBat网站的一个问题。 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). 给定两个字符串base和remove,返回基本字符串的一个版本,其中删除了删除字符串的所有实例(不区分大小写)。 You may assume that the remove string is length 1 or more. 您可以假设删除字符串的长度为1或更长。 Remove only non-overlapping instances, so with "xxx" removing "xx" leaves "x". 仅删除不重叠的实例,因此使用“xxx”删除“xx”会留下“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" 例如: 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. 我的代码给了我“这是一个FH”,因为我在代码中没有处理区分大小写。 I know that with Regex this could be done in one line. 我知道使用Regex可以在一行中完成。 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. 另外,如果我的代码更高效/更优雅,请告诉我。

String有一个equalsIgnoreCase(String s)方法。

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. 你可以使用equalsIgnoreCase方法将此语句base.substring(i,i+len_r).equals(remove)更改为base.substring(i,i+len_r).equalsIgnoreCase(remove)

hope helpful. 希望有帮助。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM