- To check if string contains special character or not or check whether first character is special character or not we can use regex or contains method of sting class.
- By using java.util.regex.Matcher class and java.util.regex.Pattern class methods we can check whether string has special character or not
- Lets see an example program on how to check a string has special character or not using java regex.
Program #1: Java example program to check string has special character or not using java regex's?
- package StringInterviewprograms;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class CheckSpecialCharacterString {
- /**
- * Check whether the String contains special character or not using java
- * @author www.instanceofjava.com
- */
- public static void main(String[] args) {
- String Str="Java String interview questions ";
- String Str1="Java String interview questions % ";
- Pattern p = Pattern.compile("[^a-z0-9 ]", Pattern.CASE_INSENSITIVE);
- Matcher m = p.matcher(Str);
- boolean check = m.find();
- if (check)
- System.out.println("String: "+Str+" contains special character");
- else
- System.out.println("String: "+Str+" does not contains any special character");
- Matcher m1= p.matcher(Str1);
- boolean flag = m1.find();
- if(flag)
- System.out.println("String: "+Str1+" contains special character");
- else
- System.out.println("String: "+Str1+" does not contains any special character");
- }
- }
- String: Java String interview questions does not contains any special character
- String: Java String interview questions % contains special character
- We can check each character of a string is special character or not without using java regex's.
- By using String class contains method and for loop we can check each character of a sting is special character or not.
- Let us see a java example program on how to check each character (including space) of a string is special character or not.
- String validation for special characters.
Program #2: Java example program to check each character of string is special character or not without using java regex's?
- package StringInterviewprograms;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class CheckSpecialCharacterString {
- /**
- * Check whether the each character of String is special character or not using java
- * @author www.instanceofjava.com
- */
- public static void main(String[] args) {
- String Str="Java String interview questions*$%";
- String specialCharacters=" !#$%&'()*+,-./:;<=>?@[]^_`{|}";
- for (int i = 0; i < Str.length(); i++) {
- if (specialCharacters.contains(Character.toString(Str.charAt(i))))
- {
- System.out.println(Str.charAt(i)+": is a special character");
- }
- }
- }
- }
Output:
- : is a special character
- : is a special character
- : is a special character
- *: is a special character
- $: is a special character
- %: is a special character
Program #3: Java example program to check each character of string is special character or not without using java regex's Using Eclipse IDE.
No comments