catching whitespace with scanner
catching whitespace with scanner
- Subject: catching whitespace with scanner
- From: Daniel Child <email@hidden>
- Date: Thu, 07 May 2009 01:05:17 -0400
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 TABS
while (![scanner isAtEnd]) {
[scanner scanUpToCharactersFromSet: spaceCharSet intoString:
&oneWord];
[separateWords addObject: oneWord];
[scanner scanCharactersFromSet: spaceCharSet intoString:
&whiteSpace]; // whiteSpace SHOULD RECEIVE SPACES/TABS HERE
if ([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
}
}
}
....
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden