Re: Parsing NSString
Re: Parsing NSString
- Subject: Re: Parsing NSString
- From: Clark Cox <email@hidden>
- Date: Tue, 25 Jan 2005 09:48:00 -0500
On Mon, 24 Jan 2005 22:02:12 -0800, Seth A. Roby <email@hidden> wrote:
> On Jan 24, 2005, at 6:17 PM, Henry Maddocks wrote:
> > - (NSArray *)componentsSeparatedByString:(NSString *)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.
It's a little crude, but you could do something like: (warning, typed
in mail client)
NSMutableString *mutableString = [[argList mutableCopy] autorelease];
//First, we replace all runs of whitespace with a single private-use
character (U+E000)
NSString *privateUseString = [NSString stringWithUTF8String: "\xEE\x80\x80"];
NSCharacterSet *whitespaceSet = [NSCharacterSet whitespaceCharacterSet];
NSRange foundRange = [mutableString rangeOfCharacterFromSet: whitespaceSet];
NSArray *result = nil;
while(foundRange.location != NSNotFound)
{
[mutableString replaceCharactersInRange: foundRange withString:
privateUseString];
foundRange = [mutableString rangeOfCharacterFromSet: whitespaceSet
options: 0 range: NSMakeRange(foundRange.location,[mutableString
length] - foundRange.location)];
}
//Then we use componentsSeparatedByString: with that private-use
character as the separator
result = [mutableString componentsSeparatedByString: privateUseString];
--
Clark S. Cox III
email@hidden
http://www.livejournal.com/users/clarkcox3/
http://homepage.mac.com/clarkcox3/
_______________________________________________
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