Java remove character from string

Hello everyone!! Welcome to flower brackets blog. In this post we are going to learn java remove character from string.

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,

Also read – learn strings in java

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 str = "abcmnopqr";
      System.out.println("Before = " + str);

      CharSequence char1 = "mno";
      CharSequence char2 = "def";

      String strReplace = str.replace(char1, char2); 
      System.out.println("After = " + strReplace);
   }
}

Output:

Before = abcmnopqr
After = abcdefpqr


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


Related Posts