Java 8 – How to remove special characters from String ?

In this article, we will learn how to remove special characters from the given String

Remove special characters from given String :

  • Using Java 1.7 version
  • Using Java 1.8 version

Special characters – other than small alphabets (az), capital alphabets (AZ) and numbers (09)

1. Remove special characters using Java 1.7 version :

  • Initially, a String contains alphanumeric & special characters
  • To remove special characters from the given String, use replaceAll() method of String which accepts 2 input-arguments
    • 1st argument is the regex pattern – allowing only alphanumeric characters [^a-zA-Z0-9]
    • 2nd argument is the replacement characters – empty string
  • Finally, print both Original String with special characters and new updated String after removing special characters to the console

RemoveSpecialCharactersFromString1.java

package in.bench.resources.java8.string.methods;

public class RemoveSpecialCharactersFromString1 {

	public static void main(String[] args) {

		// test String
		String str = "SJ23!$@asdf%hg%^^1*(bnb^7^()*";
		System.out.println("Original String with special characters :- \n" + str);


		// remove special chars using regex
		String result = str.replaceAll("[^a-zA-Z0-9]", "");
		System.out.print("\nAfter removing special characters :- \n" + result);
	}
}

Output:

Original String with special characters :- 
SJ23!$@asdf%hg%^^1*(bnb^7^()*

After removing special characters :- 
SJ23asdfhg1bnb7

2. Remove special characters using Java 1.8 version :

  • Initially, a List contains Strings with alphanumeric & special characters
  • To remove special characters from the given String in the List,
    1. Get stream for the list
    2. then map every string using replaceAll() method of String
    3. collect to a new List
  • Finally, print both original List of String with special characters and new List of String after removing special characters to the console

RemoveSpecialCharactersFromString2.java

package in.bench.resources.java8.string.methods;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class RemoveSpecialCharactersFromString2 {

	public static void main(String[] args) {

		// 1. Create List
		List<String> strings = new ArrayList<>();

		// 1.1 List of String
		strings.add("!@#$%String1^&*()_+");
		strings.add("String2!@#+_)($%^&*");
		strings.add("!@#+_)($%^&*String3");
		strings.add("@#+_)Stri$%^ng4!(&*");
		strings.add("!@#+_)($%^&String5*");


		// 1.2 print strings to the console
		System.out.println("List of String with special characters :- ");
		strings.forEach(System.out::println);


		// 2. Remove special chars using regex
		List<String> stringList = strings
				.stream()
				.map(str -> str.replaceAll("[^a-zA-Z0-9]", ""))
				.collect(Collectors.toList());


		// 2.1 print strings to the console 
		System.out.println("\nAfter removing special characters :- ");
		stringList.forEach(System.out::println);
	}
}

Output:

List of String with special characters :- 
!@#$%String1^&*()_+
String2!@#+_)($%^&*
!@#+_)($%^&*String3
@#+_)Stri$%^ng4!(&*
!@#+_)($%^&String5*

After removing special characters :- 
String1
String2
String3
String4
String5

Related Articles :

References :

Happy Coding !!
Happy Learning !!

Java 8 - How to remove last comma (,) from String ?
Java 8 – How to convert duplicate characters to Uppercase in a String ?