Java remove character from string

Let’s learn java remove character from string.

Java remove character from string

In java, String class do not have remove() method.

Remove only letters

In this example I have a string with numeric values and alphabet values.

replaceAll() method replaces each substring of this string that matches the given regular expression with the given replacement.

Here’s an example.

public class RemoveOnlyLetters
{
   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 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

To remove last character from string in java use substring() method of class String.

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 – nested classes in java


how to remove particular character from string

Let’s learn how to remove particular character from string. Here’s an example.

public class RemoveParticularCharacter
{
   public static void main(String[] args)
   {
      String strInput = "Hello world java";
      System.out.println(removeCharacter(strInput, 6));
   }
   public static String removeCharacter(String strRemove, int r)
   {
      return strRemove.substring(0, r) + strRemove.substring(r + 1);
   }
}


Output:

Hello orld java


java code to remove characters from string

In the below java program we take input from user using next() method of Scanner class.

Now this user input string is stored in String variable ‘strInput’.

In the next step user entered string is passed as parameter to removeCharacters() method.

In this method removeCharacters() characters are removed using Character.isLetter() method.

Here’s java code to remove characters from string.

import java.util.Scanner;
public class RemoveCharactersFromString
{
   public static void main(String[] args)
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter string to remove characters: ");
      String strInput = sc.next();
      strInput = RemoveCharactersFromString.removeCharacters(strInput);
      System.out.println("remove characters from string: " + strInput);
      sc.close();
   }
   static String removeCharacters(String str)
   {
      char[] ch = str.toCharArray();
      char[] ch1 = new char[ch.length];
      int a = 0, b = 0;
      while(a < ch.length)
      {
         if(!Character.isLetter(ch[a]))
         {
            ch1[b++] = ch[a];
         }
         a++;
      }
      return String.valueOf(ch1);
   }
}


Output:

Please enter string to remove characters: 16511619vxvdrgfdxv519913
remove characters from string: 16511619519913


java remove special character from string

Special characters are those which is not an alphabet or number.

Let’s learn to remove special character from string.

To remove special character we are using replaceAll() method.

replaceAll() method replaces each substring of this string that matches the given regular expression with the given replacement.

Syntax:

public String replaceAll(String regex, String replacement)


Parameters:

regex – the regular expression to which this string is to be matched.

replacement – the string to be substituted for each match.

Now let’s see an example to remove special characters from string in java.

public class RemoveSpecialCharacter
{
   public static void main(String[] args)
   {
      String strInput = "Hello$#world^%java^&.";
      strInput = strInput.replaceAll("[^a-zA-Z0-9]", " ");
      System.out.println("After remove special character from string in java: " + strInput);
   }
}


Output:

After remove special character from string in java: Hello world java


remove certain characters from string java

Here’s example on remove certain characters from string java.

import java.util.Scanner;
public class RemoveCertainCharacters
{
   public static void main(String[] args)
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter a string: ");
      String strInput = sc.next();
      System.out.println("Please enter certain characters in the above string to remove: ");
      String strRemove = sc.next();
      String strOutput = removeCharacters(strInput, strRemove);
      System.out.println("After remove certain characters from string java: ");
      System.out.println(strOutput);
      sc.close();
   }
   public static String removeCharacters(String strOriginal, String strCertain)
   {
      char[] chOriginal = strOriginal.toCharArray();
      char[] chRemove = strCertain.toCharArray();
      int start, end = 0;
      boolean[] bool = new boolean[128];
      for(start = 0; start < chRemove.length; ++start)
      {
         bool[chRemove[start]] = true;
      }
      for(start = 0; start < chOriginal.length; ++start)
      {
         if(!bool[chOriginal[start]])
         {
            chOriginal[end++] = chOriginal[start];
         }
      }
      return new String(chOriginal, 0, end);
   }
}


Output:

Please enter a string: helloworld
Please enter certain characters in the above string to remove: hw
After remove certain characters from string java: elloorld


remove first character from string java

Now let’s learn to remove first character from string using substring() method of String class.

substring() method returns a string that is a substring of this string.

The substring begins with the character at the specified index and extends to the end of this string.

In the below example we are passing 1 as an argument in substring() method which removes first character ‘c’ from string ‘corejava’.

public class RemoveFirstCharacter
{
   public static void main(String[] args)
   {
      String strInput = "corejava";
      System.out.println("After remove first character from string: " + strInput.substring(1));
   }
}


Output:

After remove first character from string: orejava


remove substring from string java

Here’s example on remove substring from string java.

class RemoveSubstringFromString
{
   public static void main(String[] args)
   {
      String str = "pqrsTUWX869";
      String strOutput = str.replaceFirst("pq", "");
      System.out.println(strOutput);
   }
}


Output:

rsTUWX869