How do I check if a string contains a list of characters?

后端 未结 8 739
小鲜肉
小鲜肉 2020-12-11 02:02

How do I check if a list of characters are in a String, for example \"ABCDEFGH\" how do I check if any one of those is in a string.

相关标签:
8条回答
  • 2020-12-11 03:06

    This is how it can be achieved using Pattern and Matcher,

    Pattern p = Pattern.compile("[^A-Za-z0-9 ]");
    Matcher m = p.matcher(trString);
    boolean hasSpecialChars = m.find();
    
    0 讨论(0)
  • 2020-12-11 03:06

    This seems like a Homework question... -_-

    You can use the String.contains() function.
    For example:

    "string".contains("a");
    String str = "wasd";
    str.contains("a");
    

    but you will need to call it once per every character you want to check for.

    0 讨论(0)
提交回复
热议问题