In this post, we will discuss how to check string contains special characters in java. We have used 3 ways like regex, ASCII value Special Characters.
What is a Special Character?
According to Computerhope, a Special character is a character other than an alphabetic or numeric character. Examples of special characters are Punctuation marks and other symbols.
How to check string contains special characters in Java?
We can check whether the given string contains any special characters or not by using the below approaches:
- Check special characters in java using ASCII value.
- Check special characters in java using regex.
- Check special characters in Java using all special characters.
Usage | Value |
ASCII | Range [32, 47], [58, 64], [91, 96] & [123, 126] of ASCII Table. |
Regex | [^a-zA-Z0-9] |
Special Characters | !@#$%&*()’+,-./:;<=>?[]^_`{|} |
Note: You can also refer to ASCII Table to get complete knowledge about ASCII values.
Read Also: How To Reverse A String In Java – Learn 5 Easy Methods
Let’s see all approaches with programs to check special characters in Strings.
Approach-1: How to check String contains special characters in java using ASCII value?
In this approach, We will use ASCII values to check whether characters of a String contain any special character or not.
As we have already given all ranges of ASCII values of special characters in the above table, we have to check for each range of ASCII values in the Java program.
Let’s see the program here.
Java program using ASCII value:
/**
* How to check special characters in java using ASCII value
*/
package cf.java.string;
import java.util.Scanner;
public class CheckSpecialCharacters {
public static void main(String[] args) {
// Declare variables
String str;
Scanner sc = new Scanner(System.in);
// Accept any String
System.out.print("Enter any String to check: ");
str = sc.nextLine();
boolean check = false;
// Use for loop to check special characters
for (int i = 0; i < str.length(); i++) {
int asciiVal = (int) str.charAt(i);
// Check whether String contains special character or not
if ((asciiVal >= 32 && asciiVal <= 47) || (asciiVal >= 58 && asciiVal <= 64)
|| (asciiVal >= 91 && asciiVal <= 96) || (asciiVal >= 123 && asciiVal <= 126)) {
check = true;
break;
}
}
if (check)
System.out.println("Given String contains special character!");
else
System.out.println("Given String doesn't contain any special characters");
}
}
Code language: Java (java)
Output:
Enter any String to check: Codingface@1234
Given String contains special character!
Code language: JavaScript (javascript)
Explanation of ASCII value program:
In the above program first, we will accept any String from the user(Line no 15 & 16). Then we have used for loop for(int i = 0; i < str.length(); i++) to iterate given String.
We have declared the asciiVal variable to store the ASCII value of each character before checking for a special character and we will use this asciiVal to check-in if( ) condition.
In the if( ) condition, we have taken multiple conditions to check for ASCII values. If String will contain any special characters, then we will change the value of the check to true. otherwise check will remain false. We have declared a boolean variable check (boolean check = false;) to use for true or false. So that by using this check variable we can display whether the given String contains any special character or not.
Finally, we have displayed the result using the print( ) method.
Read Also: How To Find Power Of A Number In Java – 3 Simple Ways
Approach-2: How to check String contains special characters in java using regex?
In this approach, we will use regex with java.util.regex.Matcher and java.util.regex.Pattern classes to check special characters in a String.
Let’s see the program here.
Java program using regex:
/**
* How to check String contains special characters in Java using regex?
*/
package cf.java.string;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CheckSpecialCharactersUsingRegex {
public static void main(String[] args) {
// Declare variables
String str;
Pattern checkPattern;
Matcher match;
boolean check;
Scanner sc = new Scanner(System.in);
// Accept any String
System.out.print("Enter any String to check: ");
str = sc.nextLine();
// Declare pattern of special characters
checkPattern = Pattern.compile("[^a-zA-Z0-9]");
// Declare matcher to match with String
match = checkPattern.matcher(str);
// Use find( ) function to check
check = match.find();
// Check whether String contains any special characters or not and display
// accordingly
if (check)
System.out.println("Given String contains special character!");
else
System.out.println("Given String doesn't contain any special characters");
}
}
Code language: Java (java)
Output:
Enter any String to check: Codingface@1234
Given String contains special character!
Code language: JavaScript (javascript)
Explanation of regex program:
In the above program, we have used the regex pattern [^a-zA-Z0-9] to check for all special characters.
First, we have compiled the given regex using compile( ) method of java.util.regex.Pattern class and then we have matched this pattern with the given String using matcher( ) method of java.util.regex.Matcher class.
Finally, we have checked that whether the given pattern of regex was found in String or not by using the find( ) function which returns type is a boolean. So find( ) the function will return either true or false and by using this check, we have displayed the result to the user.
Read Also: Java Program To Print Vowels In A String – 2 Simple Programs
Approach-3: How to check String contains special characters in java using all special characters?
In this approach, we will all special characters directly to check those special characters are present in a String or not.
Let’s check the program now.
Java program using special characters:
package cf.java.string;
import java.util.Scanner;
public class CheckSpecialCharactersUsingAllSpclChars {
public static void main(String[] args) {
// Declare variables
String str;
Scanner sc = new Scanner(System.in);
// Accept any String
System.out.print("Enter any String to check: ");
str = sc.nextLine();
String specialChars = "!@#$%&*()'+,-./:;<=>?[]^_`{|}";
boolean check = false;
// Use for loop to check special characters
for (int i = 0; i < str.length(); i++) {
String strChar = Character.toString(str.charAt(i));
// Check whether String contains special character or not
if (specialChars.contains(strChar)) {
check = true;
break;
}
}
if (check)
System.out.println("Given String contains special character!");
else
System.out.println("Given String doesn't contain any special characters");
}
}
Code language: JavaScript (javascript)
Output:
Enter any String to check: Codingface@1234
Given String contains special character!
Code language: JavaScript (javascript)
Explanation of special characters program:
In the above program first, we have declared all special characters in a String as String specialChars = “!@#$%&*()’+,-./:;<=>?[]^_`{|}”;specialChars.
Then by using for loop we have iterated the given String from index 0 to the last index. Inside for loop, we have declared a String strChar to store each character as String and then we have checked whether this String is present in specialChars String or not. If it is available then we will change the value of the check to true otherwise, it remains false.
Finally, by using the check variable we have displayed the result to the user.
Other readers also ask for:
- How to check string contains special characters in java?
- How to find special characters in a string in java?
- How to check special characters in a string in java?
- How to check if a string contains special characters in java?
- How to check if the string contains special characters in java?
FAQs
What is a Special Character?
A special character is a character other than an alphabetic or numeric character. Examples of special characters are Punctuation marks and other symbols.
How to check string contains special characters in Java?
We can check whether the given string contains any special characters or not by using the below approaches:
Check special characters in java using ASCII value.
Check special characters in java using regex.
Check special characters in Java using all special characters.
What are the Special Characters?
Special Characters include !@#$%&*()’+,-./:;<=>?[]^_`{|}.
Which regex we can use to find Special Characters?
Regex: [^a-zA-Z0-9]
2 thoughts on “How To Check String Contains Special Characters In Java?”