I have a String with name str.
str = "hi john";
Now I want to set j
char to g
. How can I do that?
You can't modify a String
directly, but you can either use a StringBuilder:
str = "hi john";
StringBuilder sb = new StringBuilder(str);
sb.setCharAt(3,'g');
str = sb.toString();
.. or convert it to a char[]
and back
str = "hi john";
char[] chars = str.toCharArray();
chars[3] = 'g';
str = new String(chars);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With