

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to check if a string contains any special character
To check if a string contains any special character, the Java program is as follows −
Example
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo { public static void main(String[] args){ String my_str="This is a sample only !$@"; Pattern my_pattern = Pattern.compile("[^a-z0-9 ]", Pattern.CASE_INSENSITIVE); Matcher my_match = my_pattern.matcher(my_str); boolean check = my_match.find(); if (check) System.out.println("Special character found in the string"); else System.out.println("Special character not found in the string"); } }
Output
Special character found in the string
A class named Demo contains the main function, where a string is defined, with some special characters. A pattern is defined that uses regular expressions to check if the string has any special characters. A Boolean value is defined, that checks to see for the same. If the value of Bool is true, a success message is printed, otherwise, a message stating the absence of these special characters is printed on the screen.
- Related Questions & Answers
- C# program to check if a string contains any special character
- Program to check if a string contains any special character in C
- Program to check if a string contains any special character in Python
- Python program to check if a string contains any unique character
- PHP program to check if a string has a special character
- Java Program to check if the String contains any character in the given set of characters
- Check if string contains special characters in Swift
- Java Program to Check if a string contains a substring
- Check if a string contains a number using Java.
- Java Program to check if the String contains only certain characters
- Java Regular expression to check if a string contains alphabet
- Python program to check if a string contains all unique characters
- How to check if a string contains only one type of character in R?
- C# Program to replace a special character from a String
- Java Program to check if any String in the list starts with a letter
Advertisements