2014-10-01 51 views
0

我遇到了我的代码的麻烦。 它在大多数情况下都有效,例如,以下是“酷”:Nagash很酷很酷,变成Nagash真的很酷。 但是,像下面这样抽取'ish':Nagash很冷静,变成Nagash就是cooh。如何从一个字符串中删除指定的词第一次出现

我很新的Java和我的代码已被写入在环路和这样的没有快捷方式的方法。如果你能回复,请你写下你的答案,回答如何用我的代码写成java。

String deleteFromString(String word, String remove) {  
    String updated = "";  

    int count = 0; 
    int start = 0; 
    int end = 0; 

    if (stringEquals(remove, "")) { 
     updated = word; 
     return updated; 
    } 

    while (count < length(word)) { 
     if (getChar(word, count) == getChar(remove, start)) { 
      if (getChar(word, count + length(remove) - 1) == getChar(remove, length(remove) - 1)) { 
       start = count; 
       end = count + length(remove) - 1; 


       println(count); 
       println(count + length(remove) - 1); 
       count = length(word); 
      } 
     } 
     count++; 
    } 

    count = 0; 
    while (count < start - 1) { 
     updated += getChar(word, count); 
     count++; 

    } 

    while (end < length(word)) { 
     updated += getChar(word, end); 
     end++; 

    } 
    println(updated); 


    return updated;   
} 
+2

启动调试器并使用输入数据导致错误结果。 – 2014-10-01 05:20:06

+0

此代码有多个必须通过的测试。每次收到一个字符串和另一个必须从原始字符中省略的字符串。 – Nagash 2014-10-01 05:21:20

+0

你的语法不是java,错了标签? – user1933888 2014-10-01 05:24:50

回答

0

你的问题就出在你的最后两个while循环。在第一个中,循环将在'remove'子串的开始之前终止两个字符,而不是之前的字符。第二,循环从'结束'字符的位置开始,因此包含它。在你的例子中,包含最后一个字符是因为最终字符的索引总是比字符串的长度小1。这给出了将删除的子字符串一个字符移到父字符串左边的效果。尝试改为:

while (count < start) { 
    updated += getChar(word, count); 
    count++; 

} 

while (end + 1 < length(word)) { 
    updated += getChar(word, end + 1); 
    end++; 

} 
+0

非常感谢您的回复:)我会在早上测试这个。请问我的代码在某些情况下是如何工作的,但不是其他情况?欢呼 – Nagash 2014-10-01 07:06:52

+0

这工作!谢谢。唯一不符合你答案的测试是当一个字被删除没有出现在字符串中。这使得第一个字母在输出到字符串时被错过了,因为count是0,start是0.我通过在主循环之后放置一个IF来检查是否开始和结束= 0,如果他们没有我刚刚返回'字'。 – Nagash 2014-10-02 00:06:53

0

您可以参考StringUtils替换方法的源代码。


public static String replace(final String text, final String searchString, final String replacement, int max) { 
    if (isEmpty(text) || isEmpty(searchString) || replacement == null || max == 0) { 
     return text; 
    } 
    int start = 0; 
    int end = text.indexOf(searchString, start); 
    if (end == INDEX_NOT_FOUND) { 
     return text; 
    } 
    final int replLength = searchString.length(); 
    int increase = replacement.length() - replLength; 
    increase = increase < 0 ? 0 : increase; 
    increase *= max < 0 ? 16 : max > 64 ? 64 : max; 
    final StringBuilder buf = new StringBuilder(text.length() + increase); 
    while (end != INDEX_NOT_FOUND) { 
     buf.append(text.substring(start, end)).append(replacement); 
     start = end + replLength; 
     if (--max == 0) { 
      break; 
     } 
     end = text.indexOf(searchString, start); 
    } 
    buf.append(text.substring(start)); 
    return buf.toString(); 
} 


public static boolean isEmpty(CharSequence cs) { 
    return cs == null || cs.length() == 0; 
} 
-1

为了简单做这种方式:

String test="Nagash is coolish"; 
test=test.replaceFirst("ish", ""); 
System.out.println(test); 

输出:

Nagash is cool 

编辑: 这里是COMPLE te代码处理strings替换号码。

public class Test { 
    public static void main(String args[]) throws Exception { 
     Test test=new Test(); 
     String result=test.computeRemove("this is demo and demo", "demo", "one"); 
     // you can pass different parameters for desired result. 
     System.out.println(result); 
    } 
    public String computeRemove(String main,String remove, String replace) { 
     main=main.replaceFirst(remove,replace); 
     return main; 
    } 
} 

更多关于这种String操作参观this链接。

+1

按OP'我的代码已被写入在环路和这样的没有捷径methods' – 2014-10-01 05:30:03

+0

@ScaryWombat什么是错的,如果我表现出更好的办法? – 2014-10-01 05:40:20

+1

@downvoters - 显示另一种方式并不完全错误。我同意,如果你不投票回答。但是在这种情况下,投票(我认为)是错误的。 – TheLostMind 2014-10-01 05:43:11

相关问题