2014-10-01 69 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

相關問題