How to replace a character in a String with another character

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

Comments

Popular posts from this blog

How to use logging in SpringBoot with code samples

Python While Loop with code samples

How to convert a List to a Set