I am interested in iterating through (re: find and replace purposes), say:
List<String> someList = new ArrayList<String>();
where someList is already populated in an earlier method, and consists of, say just a couple elements, in the fashion of, call it [a:bX, b:Xc],
where the find-and-replace String(s) of interest are, say:
String someString = "X";
String otherString = "Y";
String contentsTBD = "";
Now, ideally I thought I could've iterated over someList like so:
public void readAndReplace() {
for (int i = 0; i < someList.size(); i++) {
if (someList.get(i).contains(someString)) {
someList.get(i).replace(someString, otherString);
} else {
++i;
}
}
System.out.print(someList);
}
wherein the printout should read:
[a:bY, b:Yc]
Then, I thought this might work:
public void readAndReplace() {
for (String s : someList) {
contentsTBD += s;
}
for (int i = 0; i < contentsTBD.length(); i++) {
if (contentsTBD.contains(someString)) {
contentsTBD.replaceAll(someString, otherString);
} else {
++i;
}
}
System.out.print(contentsTBD);
}
but then quickly realized that this was nonsensical since my reference to i was lost. Any advice would be really helpful. Thank you.
replace
doesnot modify the existing String. You need to re-assign the new string. Plus it will also not modify the list. You need to create a new list. – Rohit Jain Oct 12 '12 at 5:43