On 07/05/2009, at 3:05 PM, Daniel Child wrote:
I'm trying to catch whitespace between words with a scanner. For some reason, the white space is not being recognized.Am I missing something obvious or is there a bug here? Thanks. { NSMutableArray *separateWords; NSScanner *scanner; NSCharacterSet *spaceCharSet; NSString *oneWord, *whiteSpace; unichar whiteSpaceChar; spaceCharSet = [NSCharacterSet whitespaceCharacterSet]; separateWords = [NSMutableArray array]; whiteSpace = [NSString stringWithString: @""];scanner = [NSScanner scannerWithString: words]; // words IS A PASSED IN STRING THAT CONTAINS SPACES AND TABSwhile (![scanner isAtEnd]) {[scanner scanUpToCharactersFromSet: spaceCharSet intoString: &oneWord];[separateWords addObject: oneWord];[scanner scanCharactersFromSet: spaceCharSet intoString: &whiteSpace]; // whiteSpace SHOULD RECEIVE SPACES/TABS HEREif ([whiteSpace isEqualToString: @" "]) {NSLog(@"We caught a space.\n"); // NEVER GETS CAUGHT EVEN THOUGH words DEFINITELY HAS SPACES} if (whiteSpace) { whiteSpaceChar = [whiteSpace characterAtIndex: 0];if ((whiteSpaceChar == 0x0020) || (whiteSpaceChar == 0x0009)) { // SPACE OR TAB[separateWords addObject: whiteSpace];whiteSpace = @""; // need to reset to blank or it will keep accumulating characters} } } .... }
Ran into this myself the other day - NSScanner is set to ignore whitespace characters by default, so you need to add [scanner setCharactersToBeSkipped:nil] after creating the scanner.
If you just want to split a string into words though, and you can target 10.5 or later, you can use the NSString method:
- (NSArray *) componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator
--Graham _______________________________________________ Cocoa-dev mailing list ([email protected]) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [email protected]
