0

Hello everyone,

I am having some trouble getting replaceAll(); to work. I am trying to apply it to a String array that has 4 rows and writing the portion of the code as follows

for(int i = 0; i < 4; i++){

        lines[i].replaceAll("this", "that");
       
    }

For one reason or another "this" is NOT being replaced even once by "that" ..

I have everything I know how. Can anyone help ?

greatly appreciated,

Colin

2
Contributors
2
Replies
4
Views
7 Years
Discussion Span
Last Post by squigworm
1

Remember that Strings are immutable.
What you're doing is creating a new String where "this" is replaced with "that" and then immediately you throw that String away, leaving the old one in the array.

What you are looking for is something like

array[i] = array[i].replaceAll("this", "that");
Votes + Comments
Very Very Helpful !!!!
0

Thank You , thank You , thank You , Thank you !!

for (int i = 0; i < 100000; i++)
System.out.println("THANK YOU !!!!");


Remember that Strings are immutable.
What you're doing is creating a new String where "this" is replaced with "that" and then immediately you throw that String away, leaving the old one in the array.

What you are looking for is something like

array[i] = array[i].replaceAll("this", "that");
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.