Special characters are those characters that are neither a letter nor a number. Whitespace is also not considered a special character. Examples of special characters are:- !(exclamation mark), , (comma), #(hash), etc.
Methods:
- Using Character class
- Using regular expressions
- Using contains() method
Method 1: Using Character class
The approach is as follows:
- Iterate through all the characters of the string.
- Alongside we will be checking each character for being a letter, a digit, or whitespace using the java character class.
- It is found to be none of the above signifying special characters.
- Parallelly we will maintain a counter for special character count.
- Lastly, print and display the required count or special characters as per the need.
Example
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int count = 0 ;
String s
= "!#$GeeeksforGeeks.Computer.Science.Portal!!" ;
for ( int i = 0 ; i < s.length(); i++) {
if (!Character.isDigit(s.charAt(i))
&& !Character.isLetter(s.charAt(i))
&& !Character.isWhitespace(s.charAt(i))) {
count++;
}
}
if (count == 0 )
System.out.println(
"No Special Characters found." );
else
System.out.println(
"String has Special Characters\n" + count + " "
+ "Special Characters found." );
}
}
|
Output
String has Special Characters
8 Special Characters found.
Time Complexity: O(N)
Auxiliary Space: O(N)
Method 2: Using regular expressions
- Create a regular expression that does not match any letters, digits, or whitespace.
- We will use Matcher class in accordance with regular expression with our input string.
- Now, if there exist any ‘character matches’ with the regular expression then the string contains special characters otherwise it does not contain any special characters.
- Print the result.
Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class GFG {
public static void main(String[] args)
{
String s1 = "GeeksForGeeks" ;
Pattern p = Pattern.compile(
"[^a-z0-9 ]" , Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(s1);
boolean res = m.find();
if (res)
System.out.println(
"String1 contains Special Characters" );
else
System.out.println(
"No Special Characters found in String 1" );
String s2 = "!!Geeks.For.Geeks##" ;
Matcher m2 = p.matcher(s2);
boolean res2 = m2.find();
if (res2)
System.out.println(
"String 2 contains Special Characters" );
else
System.out.println(
"No Special Characters found in String 2" );
}
}
|
Output
No Special Characters found in String 1
String 2 contains Special Characters
Time Complexity: O(N)
Auxiliary Space: O(N)
Method 3 : Using contains() method
Java
import java.io.*;
import java.util.*;
class Main{
public static void main(String[] args)
{
int count = 0 ;
ArrayList<Character> a = new ArrayList<Character>();
String b= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" ;
for ( int i= 0 ;i<b.length();i++)
{
a.add(b.charAt(i));
}
String s = "!#$GeeeksforGeeks.Computer.Science.Portal!!" ;
for ( int i = 0 ; i < s.length(); i++) {
if (!a.contains(s.charAt(i)) && !Character.isWhitespace(s.charAt(i))) {
count++;
}
}
if (count == 0 )
System.out.println( "No Special Characters found." );
else
System.out.println( "String has Special Characters\n" + count + " " + "Special Characters found." );
}
}
|
Output
String has Special Characters
8 Special Characters found.
Feeling lost in the vast world of Backend Development? It's time for a change! Join our
Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Last Updated :
03 Nov, 2022
Like Article
Save Article