How to replace a character in a String with another character
Posted On
Posted By admin
There is a replace method provided by the String class that can be used to replace a character in a String with another character. The following code snippet demonstrates this:
package demo; public class StringDemo { public static void main(String[] args) { String str = "Hello World"; String modifiedStr = str.replace('o', 'i'); System.out.println("Modified String is "+modifiedStr); } }
So the above code will replace all occurrences of the letter ‘o’ in the String “Hello World” an ‘i’ and will print the following:
Helli Wirld
If you like this post, please do let me know via the comments box below. You can also connect with me via my Facebook Page or subscribe to my Youtube channel!