Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

str.setCharAt(index,'X')

Tags:

People also ask

What does setCharAt do?

setCharAt() method sets the character at the specified index to ch. This sequence is altered to represent a new character sequence that is identical to the old character sequence, except that it contains the character ch at position index.

What is setCharAt in Java?

The setCharAt(int index, char ch) method of Java StringBuffer class is used to set the specified character at the given index. The specified character altered this sequence by replacing the old character at the given index.

How do I replace a character in a String in Java?

We can convert a char to a string object in java by using the Character. toString() method.


Is there a way to use str.charAt(index) to replace a specific char by index? Something like this:

str.setCharAt(1,'X'); // replace 2nd char with 'X'

Is there any easy way to do that?