Java remove character from string

In this post we are going to learn java remove character from string.

In java, String class do not have remove() method. However String class has replace() methods with different overloaded parameters as follows,

replace(char oldChar, char newChar)
returns a string from replacing all occurrences of oldChar with newChar.

replace(CharSequence target, CharSequence replacement) 
replace substring of string that matches the literal target sequence 
with the specified literal replacement sequence.

replaceFirst(String regex, String replacement)
replace the first substring of given string that matches the given regular expression 
with the given replacement.

replaceAll(String regex, String replacement)
replace each substring of this string that matches the given regular expression 
with the given replacement.

Java string replace(char old, char new) method example

public class ReplaceMethodDemo 
{
   public static void main(String[] args) 
   { 
      String str = "hello world!!"; 
      String strReplace = str.replace("o","e"); 
      System.out.println(strReplace);
   }
}

Output:

helle werld!!


Java.lang.String.replace() method

public class ReplaceMethodExample
{
   public static void main(String[] args)
   {
      String strInput = "Hello World Flower Brackets";
      // remove spaces from strInput
      String strOutput = strInput.replace(" ", "");
      System.out.println(strOutput);
   }
}

Output:

java remove character from string


Java String replaceFirst() Method

public class ReplaceFirstDemo 
{
   public static void main(String[] args) 
   {
      String str = new String("www.flowerbrackets.in");
      // replacing "in" with "com" 
      System.out.println(str.replaceFirst("in", "com"));
   }
}

Output:

www.flowerbrackets.com


Java string replaceAll() method example

public class ReplaceAllDemo 
{
   public static void main(String[] args) 
   {
      String str = "The flower was red. It was wonderful."; 
      String strReplace = str.replaceAll("was", "is"); 
      System.out.println(strReplace);
   }
}

Output:

The flower is red. It is wonderful.


Remove only letters

In this example I have a string with numeric values and alphabet values. What if we wanted to remove all the letters from the given string.

public class RemoveString
{
   public static void main(String[] args)
   {
      String strExample = "jd15352kLJJD55185716kdkLJJJ";   
      System.out.println("Before : " + strExample);
      strExample = strExample.replaceAll("[a-zA-Z]", "");   
      System.out.println("After : " + strExample);
   }
}

Output:

Before : jd15352kLJJD55185716kdkLJJJ
After : 1535255185716

Now all we have is just the numbers. If you want to see the space between letters. Just give space between double quotes like this,

strExample = strExample.replaceAll("[a-zA-Z]", " ");

And the output will be like this,

Before : jd15352kLJJD55185716kdkLJJJ 
After :    15352     55185716


Remove last character from string in java

To remove last character from string we use String.substring() method,

public class SubstringDemo 
{
   public static void main(String[] args) 
   {
      String strInput = "Flower Brackets!";
      String strOutput = strInput.substring(0, strInput.length() - 1); 
      System.out.println(strOutput);
   }
}

Output:

Flower Brackets


Also read – reverse string array in java