How to remove a specific character in a String in Java?

Requirement:

Remove specific character in a string in Java. For example, if the requirement is to remove “b” from “bring”, the output should be “ring”.

Download Required JAR Files:

We will use StringUtils class in Apache Common Components library. So, have a copy of commons-lang3-3.1.jar.

Java Program Example:

The Java code example for this tutorial is provided below:

import org.apache.commons.lang3.StringUtils; // to trim the string
public class removeSpecificCharacter {    
    public static void main (String[] args) {
    String myInput="bring";
    String removeCharacter="b";                     
    //This method trims the string
    System.out.println("Input: " + myInput);
    System.out.println("Trimmed : " + StringUtils.remove(myInput,removeCharacter));                 
    }
}

Example Output:


C:\Java\commons-lang3-3.1>javac -classpath .;commons-lang3-3.1.jar removeSpecificCharacter.java
C:\Java\commons-lang3-3.1>java -classpath .;commons-lang3-3.1.jar removeSpecificCharacter
Input: bring
Trimmed : ring