In Java, String
's are immutable, then methods that "alter" the String
in fact return another String
containing the changes. Therefore, you should take the return of replace
and assign to variable test
:
public String replaceFromRightToLeft(String test) {
for (int i = test.length() -1; i >= 0; i--) {
test = test.replace(test.charAt(i), '0');
System.out.println(test); // imprime o resultado parcial
}
return test;
}
If you just do test.replace(test.charAt(i), '0')
(without assigning to any variable), the return of replace
- which is the String
modified - is ignored and "lost".
Using the code above and calling replaceFromRightToLeft("12345678")
, the result will be:
12345670
12345600
12345000
12340000
12300000
12000000
10000000
00000000
But there’s a catch: replace
replaces all the occurrences of the character, then if the String
for example, "1234321"
, the result will be:
0234320
0034300
0004000
0000000
0000000
0000000
0000000
That’s because on the first call, she replaces all the characters 1
for 0
(that is, exchanges both the first and the last 1
), and the same thing for the 2
, 3
, etc..
So if the idea is to trade only the last character (and then only the penultimate, and only the antepenultimate, etc., whether or not the character is repeated in the String
), I suggest not to use replace
. An alternative is to iterate through the array of char
of own String
and keep changing him:
public String replaceFromRightToLeft(String test) {
char[] chars = test.toCharArray();
for (int i = chars.length-1; i >= 0; i--) {
chars[i] = '0';
System.out.println(new String(chars)); // imprime o resultado parcial
}
return new String(chars); // retorna a String final
}
Or use a StringBuilder
(this yes changeable, can be changed at will):
public String replaceFromRightToLeft(String test) {
StringBuilder sb = new StringBuilder(test);
for (int i = sb.length() - 1; i >= 0; i--) {
sb.setCharAt(i, '0');
System.out.println(sb.toString()); // imprime o resultado parcial
}
return sb.toString();
}
Testing both of the above options with "1234321"
the result is:
1234320
1234300
1234000
1230000
1200000
1000000
0000000
Gee!! What a sensational explanation! Thank you so much @hkotsubo!!
– Marcelo Melo