title: Using NSScanner to parse a string
body: <p>I have a string with formatting tags in it, such as <code>There are {adults} adults, and {children} children</code>. I have a dictionary which has "adults" and "children" as keys, and I need to look up the value and replace the macros with that value. This is fully dynamic; the keys could be anything (so I can't hardcode a <code>stringByReplacingString</code>).</p>
  
  <p>In the past, I've done similar things before just by looping through a mutable string, and searching for the characters; removing what I've already searched for from the source string as I go. It seems like this is exactly the type of thing NSScanner is designed for, so I tried this:</p>
  
  <pre><code>NSScanner *scanner = [NSScanner scannerWithString:format];
  NSString *foundString;
  scanner.charactersToBeSkipped = nil;
  
  NSMutableString *formatedResponse = [NSMutableString string];
  
  while ([scanner scanUpToString:@"{" intoString:&amp;foundString]) {
      [formatedResponse appendString:[foundString stringByReplacingOccurrencesOfString:@"{" withString:@""]]; //Formatted string contains everything up to the {
  
      [scanner scanUpToString:@"}" intoString:&amp;foundString];
  
      NSString *key = [foundString stringByReplacingOccurrencesOfString:@"}" withString:@""];
  
      [formatedResponse appendString:[data objectForKey:key]];
  
  }
  
  NSRange range = [format rangeOfString:@"}" options:NSBackwardsSearch];
  
  if (range.location != NSNotFound) {
      [formatedResponse appendString:[format substringFromIndex:range.location + 1]];
  }
  </code></pre>
  
  <p>The problem with this is that when my string starts with "{", then the scanner returns NO, instead of YES. (Which is what the documentation says should happen). So am I misusing NSScanner? The fact that <code>scanUpToString</code> doesn't include the string that was being searched for as part of its output seems to make it almost useless...</p>
  
  <p><strong>Can this be easily changed to do what I want, or do I need to re-write using a mutable string and searching for the characters manually?</strong></p>