Want to search in String if there is any special characters in it

前端 未结 3 1599
萌比男神i
萌比男神i 2021-01-28 19:27

I want to search any special char in java string. here is my code

    Pattern p = Pattern.compile(\"^[a-zA-Z0-9 ]\");
    Matcher m = p.matcher(\"hgbs!hf862376\"         


        
3条回答
  •  囚心锁ツ
    2021-01-28 20:10

    Pattern p = Pattern.compile("^[a-zA-Z0-9]*$");
    Matcher m = p.matcher("hgbs!hf862376");
    boolean b = m.matches(); // looking for no special characters
    
    if (!b) {
        System.out.println("sp. character is there");
    }
    

提交回复
热议问题