How to remove a character from a String via Java

In this blog post, I will be explaining how you can remove a character from a String via Java. Consider the following code snippet:

package learnjava.strings;public class RemoveCharacterDemo {public static void main(String[] args) {String str = "Hello World";String modifiedStr = str.replace("l", "");System.out.println(modifiedStr);}}

 

There is a method String.replace. This replaces the specified character with another character. Here, we are replacing the character ‘l’ with an empty character. So effectively, it is like removing the character “l”. When you run this code, you will get the following output:

Heo Word

 

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