I have a two dimensional array that contains pairs of strings. If one string is found it should replace it with its pair.
The code:
for (int i = 0; i < pairs.length; i++) {
if (name.contains(pairs[i][0])) {
name.replaceAll(pairs[i][0], abbr[i][1]);
}
}
It is not replacing the strings. What is the error?
You are neglecting to assign the result of
replaceAll, and so the modification is lost.Perhaps you want to keep the modified string as
name:Note that java
Stringobjects are immutable, so callingname.replaceAlldoesn’t modifyname, it returns a newStringwith the modifications.