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 convert a List to a Set

ArrayList Vs LinkedList

Git and Github Differences Explained In Short