【发布时间】:2016-06-27 09:47:58
【问题描述】:
我有一个 ArrayList,其中包含“倒垃圾”、“洗碗”等形式的一系列笔记。我有一个笔记类,它的方法应该找到并替换第一次出现的用户给定的字符串,例如“do”,在每个音符(如果有的话)中,并用用户给定的新字符串替换该字符串。例如:如果我有多个以“do x”开头的音符,则每个音符中的“do”应该变成“do not”。到目前为止,这是我的方法:
public void findAndReplaceFirst(String old, String newWord) {
for (int i = 0; i < notes.size(); i++) {
String note = notes.get(i);
if (note.contains(old)) {
int loc = note.indexOf(old);
int len = old.length();
String temp = note.substring(0, loc ) + note.substring(loc + len, note.length());
String newString = temp.substring(0, loc) + newWord + temp.substring(loc, temp.length());
} else {
String newString = note;
}
}
}
但是,当我运行 main 方法时,注释字符串没有改变,我不明白为什么。谁能告诉我我在哪里犯了错误?
【问题讨论】:
标签: java string arraylist intellij-idea replace