How to remove characters from a string in java

A character can be removed from a string in several ways like removing a character at the starting of string, ending of string, or at the specified position.

The replace() method can be used to remove a particular character from a string and replace it with an empty string.

The syntax is

str.replace(char old, char newChar);
  • str - it is the string from which a character has to be removed
  • old - it is the character to be removed
  • newChar - it is the character to be replaced in place of old(in this case " ")

Let us consider a string str = "Practice C programming", the character to be removed is 'd'. The example program illustrates how to remove a character using replace() method.

public static void main(String args[]) {
  String str = "Practice C programming";
  System.out.println("Length of string before removing: " + str.length());
  str = str.replace('C', '');
  System.out.println(str);
  System.out.println("Length of string after removing: " + str.length());
}

The output is

Length of string before removing: 22
Practice programming
Length of string after removing: 20

The replace() method can also be used to replace a sequence of characters. The syntax remains the same.

Let us consider a string str1 = "Practice java programming", the character sequence to be removed is 'java'. The program below illustrates how to remove a sequence of characters using replace() method.

public static void main(String args[]) {
  String str = "Practice java programming";
  System.out.println(str.replace("java", ""));
}

The output is

Practice programming

Removing a character using replaceFirst() method

The replaceFisrt() method can be used to remove a character from the string. It replaces the first occurrence of the specified character.

The syntax is

str.replaceFirst(String regex, String replace);
  • regex - the regular expression to be searched in the string, it can be a single character or a sequence of character
  • replace - it is the string to be replaced in place of regex(" " in this case).

Let us consider a String st = "Java Program example". The character to remove is "example". The example below illustrates how to remove a character using replaceFirst() method.

public static void main(String args[]) {
  String st = "Java Program example";
  System.out.println(st.replaceFirst("example", ""));
  String st1 = "Java example Program";
  // This line removes all the characters from start until example
  System.out.println(st1.replaceFirst("(.*)example", ""));
}

The output is

Java Program  
Program

Removing a character using replaceAll() method

The replaceAll() method can be used to remove all the characters specified in the method from the string.

The difference between replaceFirst() and replaceAll() is that replaceFisrt() replaces the first occurrence of the character while replaceAll() replaces all the occurrences of the specifies character.

The syntax is

str.replaceAll(String regex, String replace);
  • regex - the regular expression to be removed or replaced
  • replace - it is the string to be replaced in place of the regex

Let us consider an example str = "Java Programmer". The character to be removed is "Java". The example below illustrates how to remove a character using replaceAll() method.

public static void main(String args[]) {
  String str = "Java Programmer";
  System.out.println(str.replaceAll("Java", ""));
}

The output is

Programmer

Removing a character using substring() method

The substring() method is the built-in method of the String class. The first character of the String is present at the index zero and the last character at the index length-1.

In this method the characters can be removed in the starting, ending and in the middle of the string provided the index value of the character is known.

The syntax is

str.substring(int start, int end);
  • start - starting index of the String(printing string excluding the character to be removed)
  • end - ending index of the String

Let us consider a string str = "C Programming". The characters to be removed are 'C' at the end.

Let us take another string str1 = "Java Developer". The character to be removed is from position 0 to 4(which is Java).

The example illustrates how to remove characters from a string using the substring() method.

public static void main(String args[]) {
  String str = "C Programming";
  str = str.substring(2, str.length());
  System.out.println(str);
  String str1 = "Java Developer";
  int index = 4;
  str1 = str1.substring(4, index) + str1.substring(index + 1);
  System.out.println(str1);
}

The output is

Programming
Developer

Removing a character using StringBuilder.deleteCharAt() method

The deleteCharAt() method is a built-in method of the StringBuilder class. The index of the character to be deleted is passed as an argument to the deleteCharAt() method.

The syntax is

sb.deleteCharAt(int index);
  • sb - it is the StringBuilder object that reads a string and stores it as object
  • index - it is the index of the character to be deleted

Let us consider a string str = "A Full stack". The index position of the character to be removed is 0(which is A). The example program illustrates how to remove character using deleteCharAt() method.

public static void main(String args[]) {
  String str = "Full stcack";
  StringBuilder sb = new StringBuilder(str);
  sb.deleteCharAt(0);
  System.out.println(sb.toString());
}

The output is

Full stack

Removing a character using StringBuffer.delete() method

The delete() method is a built in method of the StringBuffer Class. The delete() method can be used to remove a character at a particular index.

The syntax is

sb.delete(int start, int end);
  • sb - it is the string buffer object which reads the string and stores it as an object
  • start - it is the starting index of the character which is to be deleted
  • end - it is the index up to which the character has to be deleted

Let us take a string str = "The Rolls royce". The index of the characters which is to be deleted is from 0 to 4(including space). The example below illustrates how to delete a character using delete() method.

public static void main(String args[]) {
  String str = "The Rolls royce";
  StringBuffer sb = new StringBuffer(str);
  sb.delete(0, 4);
  System.out.println(sb.toString());
}

The output is

Rolls royce
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions