-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathGFG.java
48 lines (39 loc) · 966 Bytes
/
GFG.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Java program to check if the string
// contains only special characters
import java.util.regex.*;
class GFG {
// Function to check if a string
// contains only special characters
public static void onlySpecialCharacters(
String str)
{
// Regex to check if a string contains
// only special characters
String regex = "[^a-zA-Z0-9]+";
// Compile the ReGex
Pattern p = Pattern.compile(regex);
// If the string is empty
// then print No
if (str == null) {
System.out.println("No");
return;
}
// Find match between given string
// & regular expression
Matcher m = p.matcher(str);
// Print Yes If the string matches
// with the Regex
if (m.matches())
System.out.println("Yes");
else
System.out.println("No");
}
// Driver Code
public static void main(String args[])
{
// Given string str
String str = "@#$&%!~";
// Function Call
onlySpecialCharacters(str);
}
}