我遇到了我的代碼的麻煩。 它在大多數情況下都有效,例如,以下是「酷」: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;
}
啓動調試器並使用輸入數據導致錯誤結果。 – 2014-10-01 05:20:06
此代碼有多個必須通過的測試。每次收到一個字符串和另一個必須從原始字符中省略的字符串。 – Nagash 2014-10-01 05:21:20
你的語法不是java,錯了標籤? – user1933888 2014-10-01 05:24:50