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: Graham Cox <email@hidden>
- Date: Tue, 13 Sep 2011 08:51:50 +1000
Typed into mail (no guarantee):
NSScanner* scanner = [NSScanner scannerWithString:input];
NSMutableString* output = [NSMutableString string];
NSString* temp;
[scanner setCaseSensitive:YES];
while( ![scanner isAtEnd])
{
if([scanner scanUpToCharactersFromSet:[NSCharacterSet uppercaseLetterCharacterSet] intoString:&temp])
{
[output appendString:temp];
if( ![scanner isAtEnd])
[output appendString:@"_"]; // underscore
}
if([scanner scanCharactersFromSet:[NSCharacterSet uppercaseLetterCharacterSet] intoString:&temp])
[output appendString:[temp lowercaseString]];
}
return output;
--Graham
On 13/09/2011, at 12:01 AM, Devraj Mukherjee wrote:
> I am trying to use NSScanner to change camel cased strings into
> underscore delimited strings, e.g featuredListingId to
> featured_listing_id
>
> I have worked our how to use NSScanner to achieve this, consider my
> implementation (implemented as a NSString category)
>
> - (NSString *) delimiterSeparatedWords
> {
> NSScanner *scanner = [NSScanner scannerWithString:self];
>
> NSString *prefix = nil;
> NSString *suffix = nil;
>
> [scanner scanCharactersFromSet:[NSCharacterSet
> lowercaseLetterCharacterSet] intoString:&prefix];
> [scanner scanCharactersFromSet:[NSCharacterSet letterCharacterSet]
> intoString:&suffix];
>
> NSString *fieldNameString = [NSString stringWithFormat:@"%@_%@",
> prefix, [suffix lowercaseString]];
> return fieldNameString;
> }
>
> obviously it only addresses the first occurrence of a capital letter.
>
> Can I ask NSScanner to keep looking for multiple occurrences?
_______________________________________________
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