Java Remove Character from String

Filed Under: Java

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

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;

  1. replace(char oldChar, char newChar): Returns a string resulting from replacing all occurrences of oldChar in this string with newChar.
  2. replace(CharSequence target, CharSequence replacement): Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.
  3. replaceFirst(String regex, String replacement): Replaces the first substring of this string that matches the given regular expression with the given replacement.
  4. 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.


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.


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'

Java Remove Spaces from String


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.


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.


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.

Comments

  1. Indrajit Das says:

    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 ?

    1. Pankaj says:

      You need to use regex for that.

  2. Nisha says:

    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.

  3. abcd says:

    how to remove the string of characters from another string
    eg: “lhe” from “hello world”.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages