title: What's the best way to trim whitespace from a string in Cocoa Touch?
body: <p>I'm looking to determine whether a string value from a user input (UITextField) is "blank" if it's not nil.  Checking if <code>[textField.text isEqualToString:""]</code> isn't quite enough because I want to avoid any blank/whitespace input (like say a few space characters).</p>
  
  <p>There does seem to be an indication of a good solution for my particular problem <a href="https://stackoverflow.com/questions/422242/simple-string-parsing-in-cocoa-objective-c-parsing-a-command-line-into-command">in this StOv post</a>.</p>
  
  <p>Basically it goes something like this, but I suspect there has to (or ought to) be a better way:</p>
  
  <pre><code>NSString *strResult;
  NSScanner* scanner = [NSScanner scannerWithString:textField.text];
  BOOL hasValidChars = [scanner scanUpToCharactersFromSet:[NSCharacterSet whitespaceAndNewlineCharacterSet] 
                                               intoString:&amp;strResult];
  
  // if hasValidChars == YES, we've got nonwhite space chars collected into strResult
  </code></pre>
  
  <p>This clearly only works for my particular circumstance, and even then it would fail if the first character was a space but the data I wanted followed. So, I realize I've been a little spoiled by Ruby, but there must be a tried and true idiom for trimming strings in Cocoa.</p>
  
  <p><strong>Aaaand the answer is already out there, my apologies:</strong></p>
  
  <p>NSString's <code>-stringByTrimmingCharactersInSet:</code> would do it:</p>
  
  <blockquote>
    <p>Returns a new string made by removing
    from both ends of the receiver
    characters contained in a given
    character set.</p>
  </blockquote>
  
  <p>I'm still curious to hear if anybody has other/preferred ways of doing this.</p>