• 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: Parsing NSString
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

References: 
 >Parsing NSString (From: "Michael A. Crawford" <email@hidden>)
 >Re: Parsing NSString (From: Henry Maddocks <email@hidden>)
 >Re: Parsing NSString (From: "Seth A. Roby" <email@hidden>)

  • Prev by Date: Trigger incomming call on modem
  • Next by Date: Re: NSMenuItem +separatorItem. Separator showing a line programatically.
  • Previous by thread: Re: Parsing NSString
  • Next by thread: Re: Parsing NSString
  • Index(es):
    • Date
    • Thread