java-string-examplesStrings

How to remove a character from a String via Java

Posted On
Posted By admin

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

 

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!

Related Post

leave a Comment