• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: catching whitespace with scanner
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: catching whitespace with scanner


  • Subject: Re: catching whitespace with scanner
  • From: Daniel Child <email@hidden>
  • Date: Thu, 07 May 2009 01:18:01 -0400

A whole while loop reduced to one line. Very cool.
Thanks also for the tip on setCharactersToBeSkipped:nil.
That was driving me nuts.

On May 7, 2009, at 1:13 AM, Graham Cox wrote:


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 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
}
}
}
....
}


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@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


References: 
 >catching whitespace with scanner (From: Daniel Child <email@hidden>)
 >Re: catching whitespace with scanner (From: Graham Cox <email@hidden>)

  • Prev by Date: Strategy for acting on changes in child objects?
  • Next by Date: Re: Printing an NSDocument
  • Previous by thread: Re: catching whitespace with scanner
  • Next by thread: Re: catching whitespace with scanner
  • Index(es):
    • Date
    • Thread