In this post, we will write a program that removes the given character from the given string.
We will discuss 3 different ways to remove the given character from the given string.
- StringBuilder based solution
- Regular expression-based solution
- Java 8, functional-style solution
3 Ways to Remove Character from String in Java
Refer the comments are self-descriptive in below program.
import java.util.regex.Pattern; import java.util.stream.Collectors; // # Remove given character public class RemoveGivenCharacter { private static final String TEXT = "JaAVaA GUIDES"; private static final char CHAR = 'a'; public static void main(String[] args) { System.out.println("Input text: \n" + TEXT); System.out.println("Character to remove: " + CHAR + "\n"); System.out.println("StringBuilder based solution:"); String resultV1 = removeCharacterV1(TEXT, CHAR); System.out.println("Result: \n" + resultV1); System.out.println(); System.out.println("Regular expression based solution:"); String resultV2 = removeCharacterV2(TEXT, CHAR); System.out.println("Result: \n" + resultV2); System.out.println(); System.out.println("Java 8, functional-style solution:"); String resultV3 = removeCharacterV3(TEXT, CHAR); System.out.println("Result: \n" + resultV3); } public static String removeCharacterV1(String str, char ch) { if (str == null || str.isEmpty()) { // or throw IllegalArgumentException throw new IllegalArgumentException("Input String can't be null"); } StringBuilder sb = new StringBuilder(); char[] chArray = str.toCharArray(); for (int i = 0; i < chArray.length; i++) { if (chArray[i] != ch) { sb.append(chArray[i]); } } return sb.toString(); } public static String removeCharacterV2(String str, char ch) { if (str == null || str.isEmpty()) { // or throw IllegalArgumentException throw new IllegalArgumentException("Input String can't be null"); } return str.replaceAll(Pattern.quote(String.valueOf(ch)), ""); } public static String removeCharacterV3(String str, char ch) { if (str == null || str.isEmpty()) { // or throw IllegalArgumentException throw new IllegalArgumentException("Input String can't be null"); } return str.chars().filter(c -> c != ch).mapToObj(c -> String.valueOf((char) c)).collect(Collectors.joining()); } }
Output
Input text:
JaAVaA GUIDES
Character to remove: a
StringBuilder based solution:
Result:
JAVA GUIDES
Regular expression based solution:
Result:
JAVA GUIDES
Java 8, functional-style solution:
Result:
JAVA GUIDES
Related Java Programs on String
- Java Program to Count Number of Duplicate Words in String
- Java Program to Count Number of Words in Given String
- Java Program to Count the Number of Occurrences of Substring in a String
- Java Program to Count the Occurrences of Each Character in String
- Java Program to Merge two String Arrays
- Java Program to Remove Duplicate Words from String
- Java Program to Reverse a String(5 ways)
- Java Program to Reverse Each Word of a String
- Java Program to Swap Two Strings
- How to Check if the String Contains only Digits
- How to Check if the String Contains only Letters
- How to Check If the String Contains Only Letters or Digits
- Java Program to Check if Input String is Palindrome
- Java Program to Find all Permutations of String
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course