While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
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?
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.
Below code snippet shows how to remove all occurrences of a character from the given string.
String str = "abcdDCBA123";
String strNew = str.replace("a", ""); // strNew is 'bcdDCBA123'
Let’s see how to remove first occurrence of “ab” from the String.
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.
String str = "abcdDCBA123";
String strNew = str.replaceAll("([a-z])", ""); // strNew is 'DCBA123'
String str = "Hello World Java Users";
String strNew = str.replace(" ", ""); //strNew is 'HelloWorldJavaUsers'
There is no method to replace or remove last character from string, but we can do it using string substring method.
String str = "Hello World!";
String strNew = str.substring(0, str.length()-1); //strNew is 'Hello World'
Here is the complete java class for the examples shown above.
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:
String after Removing 'a' = bcdDCBA123
String after Removing First 'a' = cdDCBA123
String after replacing all small letters = DCBA123
That’s all for removing character or substring from string in java program.
Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.
Sign up
tried to replace, just like you show’d but tells me an error: The method replace(char, char) in the type String is not applicable for the arguments (char, String) my code: aNum=aNum.replace(aNum.charAt(i), “”);
- slava
this is useful
- Piyush Chavare
I want to replace a few words from a String , Whenever a match will be founded it will remove that match . Example : “Learning java is not so easy but also” /* is not so much hard */ ". All that I need to replace the whole comment section ( /* ----------*/). In this case what I should do ?
- Indrajit Das
This article will provide good knowledge, who are welling to learn java. . It was great experience. Good platform to enhance our knowledge. I found a clear description in each and every topic.
- Nisha
how to remove the string of characters from another string eg: “lhe” from “hello world”.
- abcd