title: Java function to return if string contains illegal characters
body: <p>I have the following characters that I would like to be considered "illegal":</p>
  
  <p><code>~</code>, <code>#</code>, <code>@</code>, <code>*</code>, <code>+</code>, <code>%</code>, <code>{</code>, <code>}</code>, <code>&lt;</code>, <code>&gt;</code>, <code>[</code>, <code>]</code>, <code>|</code>, <code>“</code>, <code>”</code>, <code>\</code>, <code>_</code>, <code>^</code></p>
  
  <p>I'd like to write a method that inspects a string and determines (<code>true</code>/<code>false</code>) if that string contains these illegals:</p>
  
  <pre><code>public boolean containsIllegals(String toExamine) {
      return toExamine.matches("^.*[~#@*+%{}&lt;&gt;[]|\"\\_^].*$");
  }
  </code></pre>
  
  <p>However, a simple <code>matches(...)</code> check isn't feasible for this. I need the method to scan every character in the string and make sure it's not one of these characters.  Of course, I could do something <em>horrible</em> like:</p>
  
  <pre><code>public boolean containsIllegals(String toExamine) {
      for(int i = 0; i &lt; toExamine.length(); i++) {
          char c = toExamine.charAt(i);
  
          if(c == '~')
              return true;
          else if(c == '#')
              return true;
  
          // etc...
      }
  }
  </code></pre>
  
  <p>Is there a more elegant/efficient way of accomplishing this?</p>