• 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: NSScanner to find multiple occurrences of a prefix?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSScanner to find multiple occurrences of a prefix?


  • Subject: Re: NSScanner to find multiple occurrences of a prefix?
  • From: Conrad Shultz <email@hidden>
  • Date: Mon, 12 Sep 2011 16:39:37 -0700

-----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

  • Follow-Ups:
    • Re: NSScanner to find multiple occurrences of a prefix?
      • From: Chad Hulbert <email@hidden>
References: 
 >NSScanner to find multiple occurrences of a prefix? (From: Devraj Mukherjee <email@hidden>)

  • Prev by Date: Re: NSScanner to find multiple occurrences of a prefix?
  • Next by Date: Re: Problem with NSAppleScript execution
  • Previous by thread: Re: NSScanner to find multiple occurrences of a prefix?
  • Next by thread: Re: NSScanner to find multiple occurrences of a prefix?
  • Index(es):
    • Date
    • Thread