- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check that the String does not contain certain characters in Java
Let’s say the following is our string with special characters.
String str = "test*$demo";
Check for the special characters.
Pattern pattern = Pattern.compile("[^A-Za-z0-9]"); Matcher match = pattern.matcher(str); boolean val = match.find();
Now, if the bool value “val” is true, that would mean the special characters are in the string.
if (val == true) System.out.println("Special characters are in the string.");
Example
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo { public static void main(String []args) { String str = "test*$demo"; System.out.println("String: "+str); Pattern pattern = Pattern.compile("[^A-Za-z0-9]"); Matcher match = pattern.matcher(str); boolean val = match.find(); if (val == true) System.out.println("Special characters are in the string."); else System.out.println("Special characters are not in the string."); } }
Output
String: test*$demo Special characters are in the string.
The following is another example with a different input.
Example
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo { public static void main(String []args) { String str = "testdemo"; System.out.println("String: "+str); Pattern pattern = Pattern.compile("[^A-Za-z0-9]"); Matcher match = pattern.matcher(str); boolean val = match.find(); if (val == true) System.out.println("Special characters are in the string."); else System.out.println("Special characters are not in the string."); } }
Output
String: testdemo Special characters are not in the string...
- Related Articles
- Java Program to check if the String contains only certain characters
- Java Regular Expression that doesn't contain a certain String.
- Print a closest string that does not contain adjacent duplicates in C++
- How to filter rows that contain a certain string in R?
- How to split string values that contain special characters in R?
- How to check if a string only contains certain characters in Python?
- Filtering string to contain unique characters in JavaScript
- Check whether the String contains only digit characters in Java
- Java program to check order of characters in string
- MySQL query to get a field value that does not contain empty spaces?
- Program to check string is palindrome with lowercase characters or not in Python
- How to remove certain characters from a string in C++?
- Check whether the frequencies of all the characters in a string are prime or not in Python
- Check if a String is not empty ("") and not null in Java
- Program to check we can replace characters to make a string to another string or not in C++

Advertisements