To try string replace in Android, I wrote a small snippet:
public class cs{
public static void main(String[] args){
String a,c;
int b;
b=1;
c="12345";
a="12345,54321";
a.replace(c,String.valueOf(b));
System.out.println(a);
}
}
Expected Output: 12345,54321 changes to 1,54321
Actual Output: 12345,54321
。Please help。
Is the
.inc.String.valueOf(b)a typo for a comma, separating two parameters? Because it doesn’t make much sense as stated.replaceaccepts two parameters, and furthermore, it doesn’t change the string it’s executed on, it merely returns a new one, so you need to pick up that return value and reassign it to the variable: