title: Extracting characters and words from a string
body: <p>I want to scan an input line character by character and produce Strings based on valid tokens which are <code>“true”, “false”, “^” “&amp;”, “!”, “(”, “)”</code></p>
  
  <p>For example if i was given a string such as <code>String line = true &amp; ! (false ^ true)</code> </p>
  
  <p>I would have to produce the tokens <code>"true", "&amp;", "!", "(", "false", "^", "true", ")"</code></p>
  
  <p>I have been trying to use split() to divide the string into tokens and store them in an array like this <code>String[] result = line.split(" ")</code>, and then just using a bunch of if-statements inside a loop to see if the token at that index matches any of the valid tokens and just returning the token. this is kind of what i have been trying to use so far</p>
  
  <pre><code>for(int i = 0; i &lt; line.length();i++){
      if(result[i].equals("true") || result[i].equals("false") || result[i].equals("^") 
          || result[i].equals("&amp;") || result[i].equals("!") || result[i].equals("(")
          || result[i].equals(")")){
          nextToken = result[i];
  }
  </code></pre>
  
  <p>but obviously this wont extract valid tokens that are adjacent to one another, such as when the string contains something like this <code>(true</code> or this  <code>true^false</code>, which should return three tokens being <code>"true", "^", "false"</code>. Is there a way to divide a string that doesn't contain spaces or any special characters into tokens i am interested in?</p>