How Do I Make Sure a String Is Alphanumeric in Java

The idea is to use the regular expression ^[a-zA-Z0-9]*$ , which checks the string for alphanumeric characters. This can be done using the matches() method of the String class, which tells whether this string matches the given regular expression.

How do you check if the given string is alphanumeric in Java?

  1. java. util. regex. *;
  2. class AlphanumericExample.
  3. {
  4. public static void main(String… s)
  5. {
  6. String s1=”adA12″, s2=”[email protected]”;
  7. System. out. println(s1. matches(“[a-zA-Z0-9]+”));
  8. System. out. println(s2. matches(“[a-zA-Z0-9]+”));

How do you know if a character is alphanumeric?

The isalnum() method returns True if all the characters are alphanumeric, meaning alphabet letter (a-z) and numbers (0-9). Example of characters that are not alphanumeric: (space)!

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest