check if string has special characters java

Solutions on MaxInterview for check if string has special characters java by the best coders in the world

showing results for - "check if string has special characters java"
Constanza
25 Feb 2018
1 //1. Using Regex
2
3import java.util.regex.Matcher; 
4import java.util.regex.Pattern;
5
6public class JavaHungry {
7    public static void main(String args[]) {
8        String inputString = "Alive*is*Awesome$";
9        Pattern pattern = Pattern.compile("[^a-zA-Z0-9]");
10        Matcher matcher = pattern.matcher(inputString);
11        boolean isStringContainsSpecialCharacter = matcher.find();
12        if(isStringContainsSpecialCharacter)
13            System.out.println(inputString+ " contains special character");
14        else    
15            System.out.println(inputString+ " does NOT contain special character");
16    }
17}
18
19//Output:
20//Alive*is*Awesome$ contains special character
21
22
23//2. Without Using Regex 
24
25public class JavaHungry {
26    public static void main(String args[]) {
27        String inputString = "Alive*is*Awesome$";
28        String specialCharactersString = "!@#$%&*()'+,-./:;<=>?[]^_`{|}";
29        for (int i=0; i < inputString.length() ; i++)
30        {
31            char ch = inputString.charAt(i);
32            if(specialCharactersString.contains(Character.toString(ch))) {
33                System.out.println(inputString+ " contains special character");
34                break;
35            }    
36            else if(i == inputString.length()-1)     
37                System.out.println(inputString+ " does NOT contain special character");
38        }
39    }
40}
queries leading to this page
how to check if string contains special symbols in javacheck if string have special charcters javacheck whether string contain special characterhow to check if there are special characters in a stringhow to check if string contains special characters in javacheck if string contains special characters but allowing certain special charactersvalidate for special characters in javahow to check whether a string contains a special character in javahow to find special characters in string in java how to check string contains special characters in javacheck if special character in string javaprogram to check if a string contains any special character other than 22 2f 22 in javahow to check if a string contains special charactershow to check in java a string has no special character how to check special charcter and character in javastring contains special charecter check java check special characters in string java 8check if string has special characters javahow to check special symbols in javahow to check if a character is a special character in javajava special characters checkhow to cheak special character in javacheck string for special characterscheck if password contains special characters javaprogram to check if a string contains any special character except 22 2f 22 in javahow to check string contains specific special characters in javahow to check if a string include a special character javahow to check for special characters javajava find if string contains special charactershow to check if a string does not contain special characters javajava check if string contains special characterscheck not special characters in javastring contains special characters in javacheck if string has special characterscheck if string only contains special characters javahow to check for special characters in javacheck whether string contains special charactershow to check for special characters in a string javacheck string contains special characters javacheck if a character in a string is special characterhow to check string for special characters in javahow to check is given string contain any special characterhow to check special characters in javajava check if string contains numbers and special characterscheck for special characters in string in javajava check how many special characters in stringcheck if string has special characters java