How to Check If a String Contains Special Characters in Java

JavaJavaBeginner
Practice Now

Introduction

In this lab, you will learn how to check if a string contains special characters in Java using regular expressions. We will begin by defining special characters using regex patterns, understanding common special characters and their usage.

Following the definition of special characters, you will explore how to use the Pattern.matches() method to efficiently determine if a string contains any of the defined special characters. Finally, you will learn how to count the occurrences of special characters within a given string, providing a comprehensive understanding of handling special characters in Java.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("Java")) -.-> java/StringManipulationGroup(["String Manipulation"]) java(("Java")) -.-> java/SystemandDataProcessingGroup(["System and Data Processing"]) java/StringManipulationGroup -.-> java/regex("RegEx") java/SystemandDataProcessingGroup -.-> java/string_methods("String Methods") subgraph Lab Skills java/regex -.-> lab-559981{{"How to Check If a String Contains Special Characters in Java"}} java/string_methods -.-> lab-559981{{"How to Check If a String Contains Special Characters in Java"}} end

Define Special Characters with Regex

In this step, we will learn how to define special characters using regular expressions (regex) in Java. Regular expressions are powerful tools for pattern matching in strings. Special characters in regex have specific meanings and are used to define patterns.

Here are some common special characters in regex:

  • .: Matches any single character (except newline).
  • *: Matches the previous element zero or more times.
  • +: Matches the previous element one or more times.
  • ?: Matches the previous element zero or one time.
  • []: Matches any single character within the brackets.
  • |: Acts as an OR operator.
  • (): Groups elements together.
  • \: Escapes a special character, making it match the literal character.

For example, if we want to match any digit, we can use \d. If we want to match any non-digit, we use \D. Similarly, \s matches any whitespace character, and \S matches any non-whitespace character.

Let's create a simple Java program to demonstrate how to define a regex pattern for special characters.

  1. Open the HelloJava.java file in the WebIDE editor if it's not already open.

  2. Replace the entire contents of the file with the following code:

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class HelloJava {
        public static void main(String[] args) {
            String text = "Hello! This is a test string with some special characters: @#$%^&*()_+";
            // Define a regex pattern to match special characters
            String regex = "[^a-zA-Z0-9\\s]";
    
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(text);
    
            System.out.println("Original String: " + text);
            System.out.println("Regex Pattern: " + regex);
    
            System.out.print("Special characters found: ");
            while (matcher.find()) {
                System.out.print(matcher.group() + " ");
            }
            System.out.println(); // Print a newline at the end
        }
    }

    Let's understand the new parts of this code:

    • import java.util.regex.Matcher; and import java.util.regex.Pattern;: These lines import the necessary classes for working with regular expressions in Java.
    • String regex = "[^a-zA-Z0-9\\s]";: This line defines our regex pattern.
      • []: This is a character class, meaning it will match any single character within the brackets.
      • ^: When used at the beginning of a character class ([^...]), it negates the class, meaning it will match any character not in the class.
      • a-zA-Z0-9: This matches any lowercase letter (a to z), any uppercase letter (A to Z), or any digit (0 to 9).
      • \\s: This matches any whitespace character (space, tab, newline, etc.). We use \\ because \ is a special character in Java strings and needs to be escaped.
      • So, the entire pattern [^a-zA-Z0-9\\s] matches any character that is not a letter, a digit, or a whitespace character. These are our "special characters" in this context.
    • Pattern pattern = Pattern.compile(regex);: This line compiles the regex pattern into a Pattern object. Compiling the pattern improves performance if you are using the same pattern multiple times.
    • Matcher matcher = pattern.matcher(text);: This line creates a Matcher object, which is used to perform match operations on the input string (text) using the compiled pattern.
    • while (matcher.find()): This loop finds the next subsequence of the input sequence that matches the pattern.
    • matcher.group(): This returns the matched subsequence.
  3. Save the file (Ctrl+S or Cmd+S).

  4. Compile the program using the javac command in the Terminal:

    javac HelloJava.java
  5. Run the compiled program using the java command:

    java HelloJava

    You should see output similar to this:

    Original String: Hello! This is a test string with some special characters: @#$%^&*()_+
    Regex Pattern: [^a-zA-Z0-9\s]
    Special characters found: ! : @ ## $ % ^ & * ( ) _ +

You have successfully defined a regex pattern to identify special characters and used it in a Java program.

Use Pattern.matches() for Special Characters

In this step, we will explore another way to use regular expressions in Java: the Pattern.matches() method. Unlike the Matcher object we used in the previous step to find all occurrences of a pattern, Pattern.matches() checks if the entire input string matches the given regular expression.

This method is useful when you want to validate if a string conforms to a specific format, such as checking if a password contains at least one special character, or if a username only contains allowed characters.

Let's modify our HelloJava.java program to use Pattern.matches() to check if a string contains any special characters based on our previously defined regex.

  1. Open the HelloJava.java file in the WebIDE editor.

  2. Replace the existing code with the following:

    import java.util.regex.Pattern;
    
    public class HelloJava {
        public static void main(String[] args) {
            String text1 = "Hello World";
            String text2 = "Hello World!";
            // Define a regex pattern to check for the presence of special characters
            // This pattern checks if the string contains at least one character that is NOT a letter, digit, or whitespace
            String regex = ".*[^a-zA-Z0-9\\s].*";
    
            boolean containsSpecial1 = Pattern.matches(regex, text1);
            boolean containsSpecial2 = Pattern.matches(regex, text2);
    
            System.out.println("String 1: \"" + text1 + "\"");
            System.out.println("Contains special characters? " + containsSpecial1);
    
            System.out.println("String 2: \"" + text2 + "\"");
            System.out.println("Contains special characters? " + containsSpecial2);
        }
    }

    Let's look at the changes:

    • We only need to import java.util.regex.Pattern; for this method.
    • The regex pattern is now .*[^a-zA-Z0-9\\s].*. Let's break this down:
      • .*: This matches any character (.) zero or more times (*). The first .* matches anything before the special character.
      • [^a-zA-Z0-9\\s]: This is the same character class from the previous step, matching a single special character.
      • .*: This matches any character (.) zero or more times (*). The second .* matches anything after the special character.
      • Combined, .*[^a-zA-Z0-9\\s].* means "match any string that contains at least one character that is not a letter, digit, or whitespace".
    • Pattern.matches(regex, text1): This static method of the Pattern class takes the regex pattern and the input string as arguments and returns true if the entire string matches the pattern, and false otherwise.
  3. Save the file (Ctrl+S or Cmd+S).

  4. Compile the program:

    javac HelloJava.java
  5. Run the program:

    java HelloJava

    You should see the following output:

    String 1: "Hello World"
    Contains special characters? false
    String 2: "Hello World!"
    Contains special characters? true

This output shows that Pattern.matches() correctly identified that the second string contains a special character (!) while the first one does not.

Count Special Characters in String

In this final step, we will combine what we've learned about defining regex patterns and using the Matcher object to count the number of special characters in a given string. This is a practical application of regex for data analysis or validation.

We will use the same regex pattern from Step 1 ([^a-zA-Z0-9\\s]) to identify special characters and iterate through the string using the Matcher to count how many times the pattern is found.

  1. Open the HelloJava.java file in the WebIDE editor.

  2. Replace the existing code with the following:

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class HelloJava {
        public static void main(String[] args) {
            String text = "This string has 5 special characters: !@#$%";
            // Define a regex pattern to match special characters
            String regex = "[^a-zA-Z0-9\\s]";
    
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(text);
    
            int count = 0;
            while (matcher.find()) {
                count++;
            }
    
            System.out.println("Original String: \"" + text + "\"");
            System.out.println("Regex Pattern: " + regex);
            System.out.println("Number of special characters found: " + count);
        }
    }

    Here's what's happening in the new code:

    • We import both Matcher and Pattern classes again.
    • We use the same regex pattern [^a-zA-Z0-9\\s] to match individual special characters.
    • We initialize an integer variable count to 0.
    • The while (matcher.find()) loop iterates through the string, and each time the matcher.find() method finds a match for the pattern, the code inside the loop is executed.
    • Inside the loop, we increment the count variable by 1 for each special character found.
    • Finally, we print the total count of special characters.
  3. Save the file (Ctrl+S or Cmd+S).

  4. Compile the program:

    javac HelloJava.java
  5. Run the program:

    java HelloJava

    You should see the following output:

    Original String: "This string has 5 special characters: !@#$%"
    Regex Pattern: [^a-zA-Z0-9\s]
    Number of special characters found: 5

The output correctly shows that there are 5 special characters in the provided string. You have successfully used regex and the Matcher object to count specific characters in a string.

Summary

In this lab, we learned how to define special characters using regular expressions (regex) in Java. We explored common regex special characters and their meanings, such as ., *, +, ?, [], |, (), and \. We also saw how to use predefined character classes like \d, \D, \s, and \S.

We then learned how to use the Pattern.matches() method to check if a string contains special characters based on a defined regex pattern. Finally, we explored how to count the occurrences of special characters within a string using the Matcher.find() method and iterating through the matches.