Replace a character at a specific index in a string?

Viewed 1084161

I'm trying to replace a character at a specific index in a string.

What I'm doing is:

String myName = "domanokz";
myName.charAt(4) = 'x';

This gives an error. Is there any method to do this?

9 Answers

You can overwrite on same string like this

String myName = "domanokz";
myName = myName.substring(0, index) + replacement + myName.substring(index+1); 

where index = the index of char to replacement. index+1 to add rest of your string

Related