
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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 Articles
- 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
- Java Program to check if the String contains any character in the given set of characters
- PHP program to check if a string has a special character
- JAVA Menu Driven Program to Check Character is String, Number or Special Character
- Java Program to Check if a string contains a substring
- Check if string contains special characters in Swift
- Java Program to check if the String contains only certain characters
- Golang program to check if a string contains a substring
- Java Regular expression to check if a string contains alphabet
- Check if a string contains a number using Java.
- C# Program to replace a special character from a String
- How to check if a string contains only one type of character in R?

Advertisements