如何在java中查找字符串是否包含“仅”特殊字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24138371/
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-14 10:25:15  来源:igfitidea点击:

How to find if a string contains "ONLY" Special characters in java

javaregexspecial-characters

提问by shwetha

Suppose I define a String as:

假设我将一个字符串定义为:

String splChrs      = "/-@#$%^&_-+=()" ;

String inputString = getInputString();  //gets a String from end the user

I want to check if String "inputString" contains ONLYspecial characters contained in String "splChrs" i.e. I want to set a boolean flag as trueif inputStringcontained only characters present in "splChrs"

我想检查字符串“ inputString”是否包含字符串“ splChrs”中包含的特殊字符,即如果inputString仅包含“ splChrs”中存在的字符,我想将布尔标志设置为true

eg:  String inputString = "#######@";  //set boolean flag=true

And boolean flag is set as falseif it contained any letters that did not contain in "splChrs"

如果布尔标志包含“ splChrs”中不包含的任何字母,则布尔标志设置为

eg:  String inputString = "######abc"; //set boolean flag=false

采纳答案by anubhava

You can use:

您可以使用:

String splChrs = "-/@#$%^&_+=()" ;
boolean found = inputString.matches("[" + splChrs + "]+");

PS:I have rearranged your characters in splChrsvariable to make sure hyphen is at starting to avoid escaping. I also removed a redundant (and problematic) hyphen from middle of the string which would otherwise give different meaning to character class (denoted range).

PS:我已经在splChrs变量中重新排列了你的字符,以确保连字符开始避免转义。我还从字符串中间删除了一个多余的(和有问题的)连字符,否则它会给字符类(表示范围)赋予不同的含义。

Thanks to @AlanMoore for this note about character in character class:

感谢@AlanMoore 关于字符类中字符的注释:

^if it's listed first; ] if it's not listed first; [anywhere; and &if it's followed immediately by another &.

^如果它首先列出;] 如果它没有首先列出;[任何地方;而&如果它是由另一个紧跟&

回答by Cyclopes

Try this,

尝试这个,

if(inputString.matches("[" + splChrs + "]+")){

// set bool to true
}else{

// set bool to false
}

回答by java_newbie

don't invent bicycle, 'cause all those tasks are common mostly to everyone. I suggest using Apache Commons lib where it's possible: http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html#containsOnly%28java.lang.String,%20char[]%29

不要发明自行车,因为所有这些任务对每个人来说都是共同的。我建议在可能的情况下使用 Apache Commons lib:http: //commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html#containsOnly%28java.lang。字符串,%20char[]%29



containsOnly

只包含

public static boolean containsOnly(String str, char[] valid)

public static boolean containsOnly(String str, char[] valid)

Checks if the String contains only certain characters.

A null String will return false. A null valid character array will return false. An empty String (length()=0) always returns true.

 StringUtils.containsOnly(null, *)       = false
 StringUtils.containsOnly(*, null)       = false
 StringUtils.containsOnly("", *)         = true
 StringUtils.containsOnly("ab", '')      = false
 StringUtils.containsOnly("abab", 'abc') = true
 StringUtils.containsOnly("ab1", 'abc')  = false
 StringUtils.containsOnly("abz", 'abc')  = false


Parameters:
    str - the String to check, may be null
    valid - an array of valid chars, may be null 
Returns:
    true if it only contains valid chars and is non-null