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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1795402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:43:46  来源:igfitidea点击:

Check if a String contains a special character

javastringspecial-characters

提问by blue

How do you check if a String contains a special character like:

如何检查字符串是否包含特殊字符,例如:

[,],{,},{,),*,|,:,>,

回答by Michael Zilbermann

What do you exactly call "special character" ? If you mean something like "anything that is not alphanumeric" you can use org.apache.commons.lang.StringUtils class (methods IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintable).

你究竟把什么叫做“特殊字符”?如果你的意思是“任何不是字母数字的东西”,你可以使用 org.apache.commons.lang.StringUtils 类(方法 IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintable)。

If it is not so trivial, you can use a regex that defines the exact character list you accept and match the string against it.

如果不是那么简单,您可以使用正则表达式来定义您接受的确切字符列表并将字符串与其匹配。

回答by djna

All depends on exactly what you mean by "special". In a regex you can specify

一切都取决于您所说的“特殊”是什么意思。在正则表达式中,您可以指定

  • \W to mean non-alpahnumeric
  • \p{Punct} to mean punctuation characters
  • \W 表示非字母数字
  • \p{Punct} 表示标点符号

I suspect that the latter is what you mean. But if not use a [] list to specify exactly what you want.

我怀疑后者就是你的意思。但如果不使用 [] 列表来准确指定您想要的内容。

回答by Andreas Dolk

Have a look at the java.lang.Characterclass. It has some test methods and you may find one that fits your needs.

看看java.lang.Character班级。它有一些测试方法,您可能会找到适合您需求的方法。

Examples: Character.isSpaceChar(c)or !Character.isJavaLetter(c)

例子:Character.isSpaceChar(c)!Character.isJavaLetter(c)

回答by Will

Visit each character in the string to see if that character is in a blacklist of special characters; this is O(n*m).

访问字符串中的每个字符,查看该字符是否在特殊字符黑名单中;这是 O(n*m)。

The pseudo-code is:

伪代码是:

for each char in string:
  if char in blacklist:
    ...

The complexity can be slightly improved by sorting the blacklist so that you can early-exit each check. However, the string find function is probably native code, so this optimisation - which would be in Java byte-code - could well be slower.

通过对黑名单进行排序,可以稍微提高复杂性,以便您可以提前退出每个检查。但是,字符串查找函数可能是本机代码,因此这种优化(在 Java 字节码中)可能会更慢。

回答by Varun

First you have to exhaustively identify the special characters that you want to check.

首先,您必须彻底确定要检查的特殊字符。

Then you can write a regular expression and use

然后你可以写一个正则表达式并使用

public boolean matches(String regex)

回答by r3zn1k

Pattern p = Pattern.compile("[^a-z0-9 ]", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher("I am a string");
boolean b = m.find();

if (b)
   System.out.println("There is a special character in my string");

回答by Bhushan

Pattern p = Pattern.compile("[\p{Alpha}]*[\p{Punct}][\p{Alpha}]*");
        Matcher m = p.matcher("Afsff%esfsf098");
        boolean b = m.matches();

        if (b == true)
           System.out.println("There is a sp. character in my string");
        else
            System.out.println("There is no sp. char.");

回答by fastcodejava

If it matches regex [a-zA-Z0-9 ]*then there is not special characters in it.

如果它匹配正则表达式,[a-zA-Z0-9 ]*则其中没有特殊字符。

回答by Ashish Sharma

You can use the following code to detect special character from string.

您可以使用以下代码从字符串中检测特殊字符。

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DetectSpecial{ 
public int getSpecialCharacterCount(String s) {
     if (s == null || s.trim().isEmpty()) {
         System.out.println("Incorrect format of string");
         return 0;
     }
     Pattern p = Pattern.compile("[^A-Za-z0-9]");
     Matcher m = p.matcher(s);
    // boolean b = m.matches();
     boolean b = m.find();
     if (b)
        System.out.println("There is a special character in my string ");
     else
         System.out.println("There is no special char.");
     return 0;
 }
}

回答by Pir Fahim Shah

If you want to have LETTERS, SPECIAL CHARACTERS and NUMBERS in your password with at least 8 digit, then use this code, it is working perfectly

如果您想在密码中包含至少 8 位数字的字母、特殊字符和数字,请使用此代码,它运行良好

public static boolean Password_Validation(String password) 
{

    if(password.length()>=8)
    {
        Pattern letter = Pattern.compile("[a-zA-z]");
        Pattern digit = Pattern.compile("[0-9]");
        Pattern special = Pattern.compile ("[!@#$%&*()_+=|<>?{}\[\]~-]");
        //Pattern eight = Pattern.compile (".{8}");


           Matcher hasLetter = letter.matcher(password);
           Matcher hasDigit = digit.matcher(password);
           Matcher hasSpecial = special.matcher(password);

           return hasLetter.find() && hasDigit.find() && hasSpecial.find();

    }
    else
        return false;

}