Re: Parsing NSString
Re: Parsing NSString
- Subject: Re: Parsing NSString
- From: Jonathan Jackel <email@hidden>
- Date: Tue, 25 Jan 2005 22:54:33 -0500
I put this method in a category on NSString. Just feed it the right characters in the form of a character set, e.g., [NSCharacterSet characterSetWithCharactersInString:@" \t"].
- (NSMutableArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)charSet
{
NSMutableArray *components = [NSMutableArray arrayWithCapacity:[self length]/2];
NSScanner *scanner = [NSScanner scannerWithString:self];
[scanner setCharactersToBeSkipped:nil];
while(![scanner isAtEnd])
{
NSString *aString;
if([scanner scanUpToCharactersFromSet:charSet intoString:&aString])
{
[components addObject:aString];
}
[scanner scanCharactersFromSet:charSet intoString:nil];
}
return components;
}
Jonathan
On Jan 25, 2005, at 1:02 AM, Seth A. Roby wrote:
On Jan 24, 2005, at 6:17 PM, Henry Maddocks wrote:
<x-tad-bigger>- (</x-tad-bigger><x-tad-bigger>NSArray</x-tad-bigger><x-tad-bigger> *)componentsSeparatedByString:(NSString *)</x-tad-bigger>separator
But...
NSString* argList = @"command arg1 arg2 \t arg3 \t\targ4";
[argList componentsSeparatedByString:@" "];
// yields: @"arg1", @"arg2", @"\t", @"arg3", @"\t\targ4"
[argList componentsSeparatedByString:@"\t"];
// yields: @"arg1 arg2 ", @" arg3 ", @"", @"arg4"
There's no (easy) way to do a componentsSeperatedByAnyOfTheseStrings:, unfortunately.
Seth A. Roby The Amazing Llama < mail or AIM me at tallama at mac dot com>
"Life is like an exploded clown. It's really funny until you figure out what just happened."
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden