首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >给定两个字符串base和remove,返回已移除移除字符串的所有实例的基字符串的版本

给定两个字符串base和remove,返回已移除移除字符串的所有实例的基字符串的版本
EN

Stack Overflow用户
提问于 2015-08-21 05:25:51
回答 7查看 4.1K关注 0票数 -1

给定两个字符串baseremove,将返回base字符串的一个版本,其中remove字符串的所有实例都已被删除(不区分大小写)。

您可以假设删除字符串的长度为1或更长。只删除不重叠的实例,因此使用"xxx"删除"xx"会留下"x"。例如,

withoutString("Hello there", "llo") → "He there"
withoutString("Hello there", "e") → "Hllo thr"
withoutString("Hello there", "x") → "Hello there"

但是我在codingbat中的大多数测试用例都失败了。有谁能帮帮我吗?

public String withoutString(String base, String remove) {

   String result = "";
   for(int i = 0; i < base.length() - remove.length(); i++){
      if(!(base.substring(i, i + remove.length()).equalsIgnoreCase(remove))){
         result += base.substring(i, i + 1);
      }
      else{
         i = i + remove.length();
      }
   }

   return result;
}

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32128664

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档