Removing a particular character or multiple characters from a string is useful to get rid of garbage data. Moreover, this approach helps decode the encoded information in the code and assists in formatting the data efficiently. This formatting can be done by removing the start and end spaces, removing unused substrings, or correcting the spellings of strings.

Removing/Deleting Characters From a String

A character can be removed from a string in Java using the below-stated approaches:

  • substring()” Method.
  • replace()” Method.
  • StringBuilder “deleteCharAt()” Method.
  • StringBuffer “delete()” Method.

Approach 1: Remove Character From String Using “substring()” Method

This method removes the character or substring from the string based on the given index or returns the characters according to the specified indexes as intervals.

Syntax

public String substring(int startInd, int endInd);
  •  “startInd” indicates the starting index(inclusive) in the string.
  • endInd” is the ending index(exclusive) in the string.

Return Value: Targeted substring.

Now, move on to applying this method to remove a specific character from a string directly or based on the specified index as an interval:

package jbArticles;
public class Removechar {
public static void main(String[] args) {
  String st1 = "David Warner";
  String st2 = "Harry Brook" ;
  System.out.println("Given First String -> " + st1);   System.out.println("Given Second String -> " + st2);
  String remChar1 = st1.substring(1);
  System.out.println("After removing a character from string -> " + remChar1);
  String remChar2 = st2.substring(0,4) + st2.substring(5,11);
  System.out.println("After removing a character from string -> " + remChar2)
}
}

Code Explanation

  • Initialize the given strings and display them.
  • Now, apply the “substring()” method to remove the first character from the first string by referring to the character’s index.
  • After that, use the method again twice such that the characters at the given index intervals are added and returned from the second string.

Output

The first character from the first string and the specified characters from the second string are returned appropriately.

Approach 2: Remove Character From String Using the “replace()” Method

The “replace()” method locates the string for a particular character and gives a new string by replacing the particular character.

Syntax

public String replace(char locChar, char newChar)
  • locChar” denotes the character to be replaced/substituted by the “newChar”.
  • newChar” replaces the “locChar”.

Return Value

A new string with the replaced characters.

The below code replaces the target character with an empty string:

package jbArticles;
public class Removechar {
public static void main(String[] args) {
  String st = "David Warner";
  System.out.println("Given String -> " + st);
  String remChar = st.replace("i", "");
  System.out.println("After removing a character from string -> " + remChar);
}
}

Code Explanation

Initialize the string and print it. After that, apply the “replace()” method to change the specified character with an empty string, thereby eliminating the character from the string.

Output

Here, it can be seen that the particular character i.e., “i” is removed from the string.

Approach 3: Removing Character From String via the StringBuilder “deleteCharAt()” 

It belongs to the “StringBuilder” class and is used to remove/delete the character at the specified index in a string.

Syntax

public StringBuilder deleteCharAt(int ind)

Here, “ind” refers to the character’s index to remove.

Return Value

A substring of the sequence.

The below example code uses this method to remove the character at the specific index:

package jbArticles;
public class Removechar {
public static void main(String[] args) {   String st = "David Warner";
  System.out.println("Given String -> " + st);
  StringBuilder remChar = new StringBuilder(st);
  remChar.deleteCharAt(2);
  System.out.println("After removing a character from string -> " + remChar);
}
}

Code Explanation

  • Recall the steps for defining and returning the string.
  • Create a StringBuilder instance having the initialized string as its constructor’s argument.
  • Now, use the “deleteCharAt()” method to delete the character at index “2” in the string.

Output

Approach 4: Remove Character From String Using the StringBuffer “delete()” Method

The StringBuffer “delete()” method removes the substring from the starting index till the element before the specified last index.

Syntax

public StringBuffer delete(int start, int last)

Syntax Explanation

  • start” is the starting index(inclusive) in the string.
  • last” is the last index(exclusive) in the string.

Return Value

A substring of specified sequence.

The following code snippet removes the character using the StringBuffer instance and its “delete()” method by specifying the string index values as an interval:

package jbArticles;
public class Removechar {
public static void main(String[] args) {
  String st = "David Warner";
  System.out.println("Given String -> "+st);
  StringBuffer sb = new StringBuffer(st);
  sb.delete(1, 2);
  System.out.println("After removing a character from string -> " + sb);
}
}

Code Explanation

After initializing the string, create a “StringBuffer” instance having the string as its argument. Moving forward, use the “delete()” method such that the character at the second index(1) i.e., “a” is removed from the string.

Output

The second character is removed appropriately from the string based on the specified interval.

How to Eliminate Several Characters From a String?

Multiple characters can also be removed from a string using the “substring()” method by modifying the index interval in the string:

package jbArticles;
public class Removechar {
public static void main(String[] args) {
  String st1 = "David Warner";
  System.out.println("Given String -> " + st1);
  String remChar1 = st1.substring(0,3) + st1.substring(5,12);
  System.out.println("After removing a character from string -> " + remChar1); }
}

Code Explanation

Here, concatenate the “substring()” method twice such that the characters at the indexes “3” and “4” are removed from the string.

Note: The character at the specified last index “3” is not returned since it is exclusive as discussed in the method’s syntax.

Output

In this outcome, it can be seen that the characters “i” and “d” are removed accordingly.

How to Eliminate White Spaces From a String?

The “replace()” method can be used to remove whitespaces from within the string as well as the leading and trailing spaces:

package jbArticles;
public class Removechar {
public static void main(String[] args) {
  String st = " David Warner ";
  System.out.println("Given String -> " + st);
  String remChar = st.replace(" ", "");
  System.out.println("After removing a character from string -> " + remChar);
}
}

Code Explanation

  • Initialize a string having the leading and trailing whitespaces as well as spaces between the substrings. 
  • After that, apply the “replace()” method to replace the whitespaces represented as the method’s first argument with an empty string (specified as the second argument). 
  • This removes all the contained whitespaces from the string.

Output

As seen, all the whitespaces are removed from the string successfully.

How to Eliminate/Delete Only the Upper or LowerCase Characters From a String?

To remove only the lower- or upper-case characters from the string, use the regular expression i.e., regex pattern “([a-z])” or “([A-Z])“, respectively.

package jbArticles;
public class Removechar {
public static void main(String[] args) {
  String st = " David Warner ";
  System.out.println("Given String -> " + st);
  String remChar = st.replaceAll("([a-z])", "");
  System.out.println("After removing characters from string -> " + remChar);  }
}

Code Explanation

Here, the “replaceAll()” method is applied with the discussed regular expression to search for the lowercase alphabets in the string and remove them.

Output

Likewise, to remove only the uppercase characters from the string, modify the above code with the below-applied regex:

How to Remove the First and Last Characters From a String in Java?

If there is a requirement to remove only the first and last characters from a string or a substring, use the “substring()” method, as demonstrated:

package jbArticles;
public class Removechar {
public static void main(String[] args) {
  String st = "David Warner";
  System.out.println("Given String -> " + st);
  String remChar1 = st.substring(1);
  System.out.println("After removing the first character from string -> " + remChar1);
  String remChar2 = st.substring(0, st.length()-1);
  System.out.println("After removing the last character from string -> " + remChar2);
}
}

Code Explanation

  • Apply the “substring()” method and point to the first index of the string to remove the first string character.
  • Use the method again to return all the characters except the last one, thereby removing the last string character. This is achieved by returning the string’s length via the “length()” method and subtracting “1” i.e., the last character from it.

Output

How to Eliminate/Delete a Substring From a String in Java?

To remove/delete a substring from a string, the “replaceFirst()” or the “replace()” methods can be used.

Below is the code demonstration of the “replaceFirst()” method:

package jbArticles;
public class Removechar {
public static void main(String[] args) {
  String st1 = "David David Warner";
  System.out.println("Given String -> " + st1);
  String remChar1 = st1.replaceFirst("David", "");
  System.out.println("After removing a substring from string -> "+remChar1);
}
}

Code Explanation

Initialize a string having a duplicate substring and display it, after that, apply the “replaceFirst()” method to change the first occurrence of the provided substring with a blank string.

Output

The first matching substring is removed appropriately from the string.

However, to remove all the same substrings from a string at once, use the “replace()” method instead, as demonstrated below:

The specified substring is searched in the string and all occurrences are removed.

Conclusion

A character can be removed/deleted from a string by implementing the “substring()”, “replace()”, StringBuilder “deleteCharAt()”, or the StringBuffer “delete()” methods. However, the user can also choose to remove the first or last character, whitespaces, or a substring from the string by modifying the “substring()” and “replace()” methods.