📌  相关文章
📜  Java程序检查字符串是否包含特殊字符

📅  最后修改于: 2022-05-13 01:55:06.867000             🧑  作者: Mango

Java程序检查字符串是否包含特殊字符

特殊字符是那些既不是字母也不是数字的字符。空格也不被视为特殊字符。特殊字符的示例有:- !(感叹号)、,(逗号)、#(哈希)等。

方法:

  1. 使用字符类
  2. 使用正则表达式

方法一:使用字符类

方法如下:

  1. 遍历字符串的所有字符。
  2. 此外,我们将使用Java字符类检查每个字符是字母、数字还是空格。
  3. 发现上面没有任何一个表示特殊字符。
  4. 同时,我们将维护一个用于特殊字符计数的计数器。
  5. 最后,根据需要打印并显示所需的计数或特殊字符。

例子



Java
// Java Program to Check Whether String contains Special
// Characters Using Character Class
  
// Importing input output classes
import java.io.*;
  
// Main class
class GFG {
  
    // Method 1
    // Main driver method
    public static void main(String[] args)
    {
        // Declaring and initializing count for
        // special characters
        int count = 0;
  
        // Input custom string
        String s
            = "!#$GeeeksforGeeks.Computer.Science.Portal!!";
  
        // Iterating through the string
        // using standard length() method
        for (int i = 0; i < s.length(); i++) {
  
            // Checking the character for not being a
            // letter,digit or space
            if (!Character.isDigit(s.charAt(i))
                && !Character.isLetter(s.charAt(i))
                && !Character.isWhitespace(s.charAt(i))) {
                // Incrementing the countr for spl
                // characters by unity
                count++;
            }
        }
  
        // When there is no special character encounterd
        if (count == 0)
  
            // Display the print statement
            System.out.println(
                "No Special Characters found.");
        else
  
            // Special sharacter/s found then
            // Display the print statement
            System.out.println(
                "String has Special Characters\n" + count + " "
                + "Special Characters found.");
    }
}


Java
// Java Program to Check Whether String contains Special
// Characters using Regex classes
  
// Importing regex classes from java.util package to
// match a specific patteren
import java.util.regex.Matcher;
import java.util.regex.Pattern;
  
// Main class
public class GFG {
    // Main driver method
    public static void main(String[] args)
    {
        // Declaring and initializing strings randomly
        // with input characters
  
        // Custom Input as String 1
        String s1 = "GeeksForGeeks";
  
        // Creating regex pattern by
        // creating object of Pattern class
        Pattern p = Pattern.compile(
            "[^a-z0-9 ]", Pattern.CASE_INSENSITIVE);
  
        // Creating matcher for above pattern on our string
        Matcher m = p.matcher(s1);
  
        // Now finding the matches for which
        // let us set a boolean flag and
        // imposing find() method
        boolean res = m.find();
  
        // Output will be based on boolean flag
  
        // If special characters are found
        if (res)
  
            // Display this message on the console
            System.out.println(
                "String1 contains Special Characters");
  
        // If we reach here means no special characters are
        // found
        else
  
            // Display this message on the console
            System.out.println(
                "No Special Characters found in String 1");
  
        // Custom Input as String 2
        String s2 = "!!Geeks.For.Geeks##";
  
        // Creating matcher for above pattern on our string
        // by creating object of matcher class
        Matcher m2 = p.matcher(s2);
  
        // Finding the matches using find() method
        boolean res2 = m2.find();
  
        // If matches boolean returns true
        if (res2)
  
            // Then print and display thta special
            // characters are found
            System.out.println(
                "String 2 contains Special Characters");
  
        // If not
        else
  
            // Then Display the print statement below
            System.out.println(
                "No Special Characters found in String 2");
    }
}


输出
String has Special Characters
8 Special Characters found.

方法二:使用正则表达式

  1. 创建一个不匹配任何字母、数字或空格的正则表达式。
  2. 我们将根据正则表达式使用 Matcher 类与我们的输入字符串。
  3. 现在,如果正则表达式存在任何“字符匹配”,则字符串包含特殊字符,否则不包含任何特殊字符。
  4. 打印结果。

Java

// Java Program to Check Whether String contains Special
// Characters using Regex classes
  
// Importing regex classes from java.util package to
// match a specific patteren
import java.util.regex.Matcher;
import java.util.regex.Pattern;
  
// Main class
public class GFG {
    // Main driver method
    public static void main(String[] args)
    {
        // Declaring and initializing strings randomly
        // with input characters
  
        // Custom Input as String 1
        String s1 = "GeeksForGeeks";
  
        // Creating regex pattern by
        // creating object of Pattern class
        Pattern p = Pattern.compile(
            "[^a-z0-9 ]", Pattern.CASE_INSENSITIVE);
  
        // Creating matcher for above pattern on our string
        Matcher m = p.matcher(s1);
  
        // Now finding the matches for which
        // let us set a boolean flag and
        // imposing find() method
        boolean res = m.find();
  
        // Output will be based on boolean flag
  
        // If special characters are found
        if (res)
  
            // Display this message on the console
            System.out.println(
                "String1 contains Special Characters");
  
        // If we reach here means no special characters are
        // found
        else
  
            // Display this message on the console
            System.out.println(
                "No Special Characters found in String 1");
  
        // Custom Input as String 2
        String s2 = "!!Geeks.For.Geeks##";
  
        // Creating matcher for above pattern on our string
        // by creating object of matcher class
        Matcher m2 = p.matcher(s2);
  
        // Finding the matches using find() method
        boolean res2 = m2.find();
  
        // If matches boolean returns true
        if (res2)
  
            // Then print and display thta special
            // characters are found
            System.out.println(
                "String 2 contains Special Characters");
  
        // If not
        else
  
            // Then Display the print statement below
            System.out.println(
                "No Special Characters found in String 2");
    }
}
输出
No Special Characters found in String 1
String 2 contains Special Characters