title: Java - what is the best way to check if a STRING contains only certain characters?
body: <p>I have this problem: I have a <code>String</code>, but I need to make sure that it <em>only</em> contains letters <strong>A-Z</strong> and numbers <strong>0-9</strong>. Here is my current code:</p>
  
  <pre><code>boolean valid = true;
  for (char c : string.toCharArray()) {
      int type = Character.getType(c);
      if (type == 2 || type == 1 || type == 9) {
          // the character is either a letter or a digit
      } else {
          valid = false;
          break;
      }
  }
  </code></pre>
  
  <p>But what is the best and the most efficient way to implement it?</p>