Java 给定两个字符串,base和remove,返回基本字符串的一个版本,其中remove字符串的所有实例都已删除
给定两个字符串,Java 给定两个字符串,base和remove,返回基本字符串的一个版本,其中remove字符串的所有实例都已删除,java,string,Java,String,给定两个字符串,base和remove,返回base字符串的一个版本,其中remove字符串的所有实例都已删除(不区分大小写) 您可以假定删除字符串的长度为1或更长。仅删除不重叠的实例,因此使用“xxx”删除“xx”会留下“x”。 比如说, withoutString("Hello there", "llo") → "He there" withoutString("Hello there", "e") → "Hllo thr" withoutString("Hello there", "x")
base
和remove
,返回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;
}
public String withoutString(字符串基,字符串移除){
字符串结果=”;
for(int i=0;i
在阅读本答案中的其他内容之前,请注意,您应该阅读本文: 它将帮助你解决像这样的问题,并在你未来的编程生活中。也就是说:
这段代码有两个问题
i
无论发生什么情况,每次都会增加一个位置。如果未找到子字符串,则希望将其前进是正确的,但问题是将其前进太多空格。因此,与其使用i+remove.length()
,不如使用:
i = i + remove.length() - 1;
这是一条鱼
正在为你而破碎。您可以包括一个特殊的检查,然后运行到列表的末尾,例如
for (int i = 0; i < base.length(); i++) {
if(i > base.length() - remove.length()) {
result += base.substring(i, base.length());
break;
for(int i=0;ibase.length()-remove.length()){
结果+=base.substring(i,base.length());
打破
public String withoutString(String base, String remove) {
String result = "";
for (int i = 0; i < base.length(); i++) {
if (i > base.length() - remove.length()) {
result += base.substring(i, base.length());
break;
} else {
String substring = base.substring(i, i + remove.length());
if (!(substring.equalsIgnoreCase(remove))) {
result += base.charAt(i);
} else {
i = i + remove.length() - 1;
}
}
}
return result;
}
public String withoutString(字符串基,字符串移除){
字符串结果=”;
对于(int i=0;ibase.length()-remove.length()){
结果+=base.substring(i,base.length());
打破
}否则{
String substring=base.substring(i,i+remove.length());
if(!(子字符串.相等信号案例(删除))){
结果+=基本特征(i);
}否则{
i=i+remove.length()-1;
}
}
}
返回结果;
}
字符串对象中有一个函数,可以使用常规表达式替换或删除字符串片段
public String remove(String base, String remove) {
return base.replaceAll(getRegExp(remove), "");
}
private String getRegExp(String remove) {
StringBuilder regExp = new StringBuilder();
for (Character caracter : remove.toCharArray()) {
regExp.append("[").append(Character.toLowerCase(caracter))
.append(Character.toUpperCase(caracter)).append("]");
}
return regExp.toString();
}
我的答案是有效的。不过似乎更简单。期待一个更好的答案
public String withoutString(String base, String remove) {
String str = "";
for(int i = 0; i < base.length()-remove.length()+1;i++){
if(base.substring(i,i+remove.length()).equalsIgnoreCase(remove)){
base = base.substring(0,i)+base.substring(i+remove.length());
i--;
}
}
return base;
}
public String withoutString(字符串基,字符串移除){
字符串str=“”;
对于(int i=0;i
这对我很有用
public String withoutString(字符串基,字符串移除){
字符串结果=”;
对于(int i=0;ipublic String withoutString(String base,String remove){
字符串newStr=“”;
int bLength=base.length();
int rLength=remove.length();
对于(int i=0;i 如果(i以下是解决此问题的另一种方法:
public String withoutString(String base, String remove) {
return base.replaceAll("(?i)" + remove, "");
}
这是我对这个问题的回答
public static String withoutString(String str, String D) {
String temp = "";
for (int i = 0; i < str.length(); i++) {
temp += str.substring(i, i+1);
if (temp.length() >= D.length() && temp.substring(temp.length() - D.length() , temp.length()).equalsIgnoreCase(D)) {
temp = temp.substring(0, temp.length() - D.length());
}
}
return temp;
}
public static String withoutString(String str,String D){
字符串temp=“”;
对于(int i=0;i=D.length()&&temp.substring(temp.length()-D.length(),temp.length()).equalsIgnoreCase(D)){
temp=temp.substring(0,temp.length()-D.length());
}
}
返回温度;
}
我建议您在本地PC上设置这些测试,并对它们进行调试,看看哪里出了问题。我们可以提供答案-但您不会从中学习…相反,我可以给您以下提示:使用while(I
并手动适当增加I
而不是for
循环使用while(I
(if case需要不同于else case的增量)。这里有很多关于这个问题的答案,事实上……您对他们的任何解决方案都不感兴趣,并且想开创自己的解决方案吗?这是我使用StringBuilder的解决方案。请在回答中使用“编辑”而不是“注释”来编写说明
public String withoutString(String base, String remove) {
return base.replaceAll("(?i)" + remove, "");
}
public static String withoutString(String str, String D) {
String temp = "";
for (int i = 0; i < str.length(); i++) {
temp += str.substring(i, i+1);
if (temp.length() >= D.length() && temp.substring(temp.length() - D.length() , temp.length()).equalsIgnoreCase(D)) {
temp = temp.substring(0, temp.length() - D.length());
}
}
return temp;
}