Java程序检查字符串是否包含任何特殊字符
要检查字符串是否包含任何特殊字符,Java程序如下所示:
示例
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Demo {
public static void main(String[] args){
String my_str="This is a sample only !$@";
Pattern my_pattern = Pattern.compile("[^a-z0-9 ]", Pattern.CASE_INSENSITIVE);
Matcher my_match = my_pattern.matcher(my_str);
boolean check = my_match.find();
if (check)
System.out.println("Special character found in the string");
else
System.out.println("Special character not found in the string");
}
}输出结果
Special character found in the string
名为Demo的类包含主函数,该函数定义了带有一些特殊字符的字符串。定义了一种模式,该模式使用正则表达式检查字符串是否具有任何特殊字符。定义了一个布尔值,该值检查是否相同。如果Bool的值为true,则打印成功消息,否则,在屏幕上打印一条消息,指出缺少这些特殊字符。
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短