File: CheckSpecialCharacters.java
package net.codermag.sample;
public class CheckSpecialCharacters {
public static void main(String[] args) {
String s = "HelloWorld";
System.out.println("Contains no special Chars: "+s.matches("[a-zA-Z0-9]*"));
String s1 = "Hell@ W@rld..%$#*@*@!!";
System.out.println("Contains no special Chars: "+s1.matches("[a-zA-Z0-9]*"));
//Remove all special characters
System.out.println(s1.replaceAll("[^\\w]", ""));
//Remove all special characters except space
System.out.println(s1.replaceAll("[^\\w\\s]", ""));
}
}
No comments:
Post a Comment
If you have any doubts or questions, please let us know.