How to find special characters in a string in java | Java program to check and get special characters in a String
Program to Find Special Characters in a String (Without Regex)
In this section, you will learn how to find special characters from a string in Java along with counting the total number of special characters in a given string, without using regular expressions. This approach uses ASCII value checking.
Overview
This Java program demonstrates an alternative approach to finding special characters in a string by checking ASCII values of each character. This method is useful when you want to avoid regex or need more control over character classification.
Complete Program
import java.util.Scanner;
class FindSpecialCharacters {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
String str;
char find_special_charactetr[] = new char[1000];
System.out.print("\nEnter a string :");
str = input.nextLine();
int p, special_charactetr = 0;
char a[] = str.toCharArray();
int len = a.length;
for (int i = 0; i < len; i++) {
p = a[i];
if (p >= 65 && p <= 90 || p >= 97 && p <= 122 || p == 32 || p >= 48 && p <= 57) {
continue;
} else {
find_special_charactetr[special_charactetr] = a[i];
special_charactetr++;
}
}
if (special_charactetr > 0) {
System.out.println("\n\n\t______________ OUTPUT ______________");
System.out.println("\nSpecial characters is :"+String.valueOf(find_special_charactetr));
System.out.println("There are "+special_charactetr+" special characters");
System.out.println("\n\t------------------------------------");
} else {
System.out.println("\n\n\t______________ OUTPUT ______________");
System.out.println("\n Note that : No any special characters in : {"+str+"}");
System.out.println("\n\t------------------------------------");
}
}
}
Code Explanation
1. ASCII Value Check
if (p >= 65 && p <= 90 || p >= 97 && p <= 122 || p == 32 || p >= 48 && p <= 57)
This condition checks if a character is:
- 65-90: Uppercase letters (A-Z)
- 97-122: Lowercase letters (a-z)
- 32: Space character
- 48-57: Digits (0-9)
If the character doesn't match any of these ranges, it's considered a special character.
2. Character Array Conversion
char a[] = str.toCharArray();
Converts the string into a character array for easy iteration and ASCII value checking.
3. Special Character Collection
else {
find_special_charactetr[special_charactetr] = a[i];
special_charactetr++;
}
When a special character is found, it's stored in the special character array and the counter is incremented.
ASCII Value Reference
| Character Type | ASCII Range | Example Characters |
|---|---|---|
| Uppercase Letters | 65-90 | A, B, C, ..., Z |
| Lowercase Letters | 97-122 | a, b, c, ..., z |
| Digits | 48-57 | 0, 1, 2, ..., 9 |
| Space | 32 | " " |
Sample Outputs
Output 1: String with Special Characters
Enter a string : #Welc@me t@ c@derwebsite!
______________ OUTPUT ______________
Special characters is : #@@@!
There are 5 special characters
------------------------------------
Output 2: String without Special Characters
Enter a string : Welcome to coderwebsite
______________ OUTPUT ______________
Note that : No any special characters in : {Welcome to coderwebsite}
------------------------------------
ASCII Method vs Regex Method
Advantages
- • No regex knowledge required
- • Simple and straightforward logic
- • Easy to customize character classification
- • More control over which characters are considered special
Disadvantages
- • Longer code compared to regex
- • Requires memorizing ASCII values
- • Less flexible than regex patterns
- • More prone to errors in range definition
Summary
This program demonstrates a manual approach to finding special characters using ASCII value checking. This method is educational and helps understand how characters are represented in computers.
For production code, the regex approach (Pattern.compile("[^a-z0-9 ]")) is generally more concise and maintainable, but this ASCII-based approach provides valuable insights into character encoding.