views:

19

answers:

1

This is the code that I would use if it was always single spaces in between words. Since I have multiple spaces in between some words how can my code be changed to remove the extra spaces when using componentsSeparatedBySring. I'm new to OBjective-C so any help would be greatly appreciated!

Here is my code: NSString *myString = @"One Two Three Four Five"; NSArray *myArray = [myString componentsSeparatedByString: @" "];

+1  A: 

Use NSScanner instead:

NSMutableArray *results = [NSMutableArray array];
NSScanner      *scanner = [NSScanner scannerWithString:input];
NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:@" "];

while ([scanner isAtEnd] == NO) 
{
    NSString *string;
    [scanner scanUpToCharactersFromSet:charSet intoString:&string];
    [results addObject:string];
}
Georg Fritzsche