Sometimes we have to remove character from String in java program. But java String class doesn’t have remove()
method. So how would you achieve this?
Java Remove Character from String
If you notice String class, we have replace()
methods with different variations. Let’s see what all overloaded replace() methods String class has;
replace(char oldChar, char newChar)
: Returns a string resulting from replacing all occurrences of oldChar in this string with newChar.replace(CharSequence target, CharSequence replacement)
: Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.replaceFirst(String regex, String replacement)
: Replaces the first substring of this string that matches the given regular expression with the given replacement.replaceAll(String regex, String replacement)
: Replaces each substring of this string that matches the given regular expression with the given replacement.
So can we use replace('x','');
? If you will try this, you will get compiler error as Invalid character constant
. So we will have to use other replace methods that take string, because we can specify “” as empty string to be replaced.
Java String Remove Character Example
Below code snippet shows how to remove all occurrences of a character from the given string.
1 2 3 4 |
String str = "abcdDCBA123"; String strNew = str.replace("a", ""); // strNew is 'bcdDCBA123' |
Java Remove substring from String
Let’s see how to remove first occurrence of “ab” from the String.
1 2 3 4 |
String str = "abcdDCBA123"; String strNew = str.replaceFirst("ab", ""); // strNew is 'cdDCBA123' |
Notice that replaceAll
and replaceFirst
methods first argument is a regular expression, we can use it to remove a pattern from string. Below code snippet will remove all small case letters from the string.
1 2 3 4 |
String str = "abcdDCBA123"; String strNew = str.replaceAll("([a-z])", ""); // strNew is 'DCBA123' |
Java Remove Spaces from String
1 2 3 4 |
String str = "Hello World Java Users"; String strNew = str.replace(" ", ""); //strNew is 'HelloWorldJavaUsers' |
Java Remove Last Character from String
There is no method to replace or remove last character from string, but we can do it using string substring method.
1 2 3 4 |
String str = "Hello World!"; String strNew = str.substring(0, str.length()-1); //strNew is 'Hello World' |
Java String Remove Character and String Example
Here is the complete java class for the examples shown above.
1 2 3 4 5 6 7 8 9 10 11 |
package com.journaldev.examples; public class JavaStringRemove { public static void main(String[] args) { String str = "abcdDCBA123"; System.out.println("String after Removing 'a' = "+str.replace("a", "")); System.out.println("String after Removing First 'a' = "+str.replaceFirst("ab", "")); System.out.println("String after replacing all small letters = "+str.replaceAll("([a-z])", "")); } } |
Output produced by above program is:
1 2 3 4 5 |
<span style="color: #008000;"><strong><code> String after Removing 'a' = bcdDCBA123 String after Removing First 'a' = cdDCBA123 String after replacing all small letters = DCBA123 </code></strong></span> |
That’s all for removing character or substring from string in java program.