Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 检查字符串是否具有特殊字符_Java_Regex - Fatal编程技术网

Java 检查字符串是否具有特殊字符

Java 检查字符串是否具有特殊字符,java,regex,Java,Regex,如何检查字符串中的特殊字符,如$%#和数字? 我只想要chars,比如abcd 我能想到的唯一方法是制作字母表的arraylist,并检查每个索引是否在arraylist中 谢谢正则表达式可能是您最好的选择: Pattern p = Pattern.compile("[a-zA-Z]"); Matcher m = p.matcher(sourceString); boolean b = m.matches(); 您可以尝试使用正则表达式: Pattern p = Pattern.comp

如何检查字符串中的特殊字符,如$%#和数字? 我只想要chars,比如abcd

我能想到的唯一方法是制作字母表的arraylist,并检查每个索引是否在arraylist中


谢谢

正则表达式可能是您最好的选择:

 Pattern p = Pattern.compile("[a-zA-Z]");
 Matcher m = p.matcher(sourceString);
 boolean b = m.matches();
您可以尝试使用正则表达式:

Pattern p = Pattern.compile("[^a-zA-Z]");
if(p.matcher(string).find()){
//something is not a letter
}

您还可以通过调用sourceSting.matches(“[a-zA-Z]”)@Zach-。。。以每次重新编译模式为代价。(无可否认,@lukiffer的代码并没有提升模式的创建。)