How to check if only chosen characters are in a string?

后端 未结 4 1426
别跟我提以往
别跟我提以往 2020-12-30 06:19

What\'s the best and easiest way to check if a string only contains the following characters:

abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_         


        
4条回答
  •  青春惊慌失措
    2020-12-30 06:38

    if (string.matches("^[a-zA-Z0-9_]+$")) {
      // contains only listed chars
    } else {
      // contains other chars
    }
    

提交回复
热议问题