Re: NSScanner to find multiple occurrences of a prefix?
Re: NSScanner to find multiple occurrences of a prefix?
- Subject: Re: NSScanner to find multiple occurrences of a prefix?
- From: Chad Hulbert <email@hidden>
- Date: Tue, 13 Sep 2011 09:25:21 -0400
- Thread-topic: NSScanner to find multiple occurrences of a prefix?
There may be edge cases you must deal with unless you can absolutely
guarantee the form of your input. For example, while Cocoa uses lower camel
case in most places, it also contains method names like +[NSURL
URLWithString:].
-Chad
On 9/12/11 7:39 PM, "Conrad Shultz" <email@hidden> thusly
spake:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On 9/12/11 7:01 AM, Devraj Mukherjee wrote:
>> Hi all,
>>
>> I am trying to use NSScanner to change camel cased strings into
>> underscore delimited strings, e.g featuredListingId to
>> featured_listing_id
>
> In addition to the responses you have already received, if you are
> using a sufficiently new SDK you can use regexes to accomplish your goal:
>
>
>
> NSString *input = @"featuredListingId";
>
> NSError *error = nil;
>
> NSRegularExpression *regex = [NSRegularExpression
> regularExpressionWithPattern:@"([a-z])([A-Z])" options:0 error:&error];
>
> NSString *result = [[regex stringByReplacingMatchesInString:input
> options:0 range:NSMakeRange(0, [input length]) withTemplate:@"$1_$2"]
> lowercaseString];
>
>
>
> (Clearly you would need to handle errors, etc., as appropriate for
> your situation.)
>
> Regular expressions typically run slowly in relative terms, so if you
> are doing a lot of these conversions you would probably want to
> profile performance too.
>
>
> - --
> Conrad Shultz
>
> Synthetiq Solutions
> www.synthetiqsolutions.com
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.7 (Darwin)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iD8DBQFObpg5aOlrz5+0JdURAnrBAJ9kH0BeBVAbp9xKLVQE1RAgR1xMkwCeOwSC
> SRZfXZa4Hp28J71FtdcK7TU=
> =qLaC
> -----END PGP SIGNATURE-----
> _______________________________________________
>
> 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
Chad Hulbert
Software Engineer
Xerox Corporation
800 Phillips Rd
Webster, NY 14580
p 585.427.3295 (8*707.3295)
_______________________________________________
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