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 (a–z), capital alphabets (A–Z) and numbers (0–9)
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,
- Get stream for the list
- then map every string using replaceAll() method of String
- 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 :
- Java 8 – Count and print number of lines and words in a text file
- Java 8 – Count and print number of repeated word occurrences in a text file
- Java 8 – Count and print number of repeated character occurrences in a String
- Java 8 – Count and print number of Vowels and Consonants in a String
- Java 8 – Reverse each words in a String using Stream and Collectors
- Java 8 – Reverse complete/entire String using Stream and Collectors
- Java 8 – Remove input-ted Character from a given String
- Java 8 – How to split a String and Collect to any Collection ?
- Java 8 – How to check whether given String contains only Alphabets or Letters ?
- Java 8 – How to check whether given String contains only Digits ?
- Java 8 – How to check whether given String contains Alphanumeric characters only ?
- Java 8 – How to convert first character of every word to Uppercase ?
- Java 8 – How to convert duplicate characters to Uppercase in a String ?
- Java 8 – How to remove special characters from String ?
References :
- https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html
- https://docs.oracle.com/javase/8/docs/api/java/util/List.html
- https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html
Happy Coding !!
Happy Learning !!