Java

How to Remove a Character From String in Java

Strings in Java are immutable, which means that they cannot be changed. Every string is the object of the String class in Java which belongs to the java.lang package. While using string, there exist chances that you want to remove a particular string’s character. String class does not have such a remove() method; however, it has certain other methods that assist in removing a character from a string.

This tutorial will explain the methods to remove a string’s character in Java.

How to Remove a Character From String in Java?

To remove a string’s character, you can use:

  • replace() method
  • substring() method
  • deleteCharAt() method

We will check out each of the mentioned methods one by one!

Method 1: Remove a Character From String Using Java replace() Method

The “replace()” method outputs a new string by replacing the existing character with the new one. In the case of removing a character, the new returned string will remove the existing character from the string. The replace() method’s syntax is listed below.

Syntax

replace(char oldCharacter, char newCharacter)

The replace() method takes two parameters, one that will be removed, represented as “oldCharacter” and the second parameter “newCharacter” as a replaced character.

Example
Here, we will remove the white space from a string. Firstly, we’ll create a variable of type String called “strg”:

String strg= "Linux Hint" ;
System.out.println("Actual String: " + strg);

Then, we will call the “replace()” method by passing two parameters, one will be the white space that will be removed from the string, and the second will be an empty parameter. The resultant string will be stored in “newStrg”:

String newStrg= strg.replace(" ", "");  
System.out.println("Resultant String: " + newStrg);

Output indicates that the white space from the given String is removed:

Want to remove a specific character based on its position? If yes, proceed to the following section.

Method 2: Remove a Character From String Using Java substring() Method

While removing a specific character by utilizing the “substring()” method, we have to pass the positions of the character that will split the string and then concatenate it after removing that specific character. The syntax of the substring() method to remove a character from a string is given below.

Syntax

substring(startingPositionOfString,PositionOfRemoveChar) + substring(positionOfNextChar);

The first substring() method accepts two parameters; the first will be the starting position of a string and the second parameter as the position of the character that will be removed. It returns a new substring that splits the existing string. Then, the second substring() returns a new string that starts from the index next to the removed character. Lastly, the two new strings will be concatenated using the “+” operator.

Example
In this example, we want to remove a character “e” from the “strg” string that is present at the first index:

String strg= "Leinux Hint" ;
System.out.println("Actual String: " + strg);

We will call the substring() method and pass “0” as the starting index of the string, and “1” is the position of character “e” that we want to remove. Then, concatenate the returned string with the new substring that starts from the 2nd index:

String newStrg= strg.substring(0, 1) + strg.substring(2);  
System.out.println("Resultant String: " + newStrg);

As you can see, we have successfully removed the character “e” from the given string:

To perform the same operation more efficiently, use the “deleteCharAt()” method.

Method 3: Remove a Character From String Using Java deleteCharAt() Method

Another method to remove a character from a string is “deleteCharAt()”. This method belongs to the Java “StringBuilder” class. It takes only one character as a parameter, the index of the character that will be removed, and returns the resultant string.

Syntax

deleteCharAt(positionOfCharacter);

Example
We will now create an object of the “StringBuilder” class named “newStrg” and call the “deleteCharAt()” method by passing the position of the character as “1” to remove the “e” character:

StringBuilder newStrg = new StringBuilder(strg);
newStrg.deleteCharAt(1);
System.out.println("Resultant String: " + newStrg);

Output

We provided the easiest methods to remove a character from a string in Java.

Conclusion

To remove a character from a string, there is no special remove() method offered by the String class. So, we can use some other methods to perform this operation, such as replace() method, substring() method, and the deleteCharAt() method. This tutorial explained the methods to remove a Java string’s character with examples.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.