Java – check string contents

I need to perform the following checks:

If mystring contains()

Any other character

Letters A-Z, A-Z, "_, "–", numbers 0-9

Then

What is the correct Java syntax for this check?

Solution

You can use regular expressions

Pattern badChar = Pattern.compile("[^A-Za-z0-9_-]");
if(badChar.matcher(myString).find()) {
  // ...
}

This pattern will match any single character except letters, numbers, underscores and hyphens

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>