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
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”:
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”:
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
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:
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:
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
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:
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.