开发者

iPhone dev: Replace uppercase characters in NSString with space and downcase

开发者 https://www.devze.com 2022-12-26 06:12 出处:网络
I have: NSString *promise = @\"thereAreOtherWorldsThanThese\"; which I\'m trying to transform into the string:

I have:

NSString *promise = @"thereAreOtherWorldsThanThese";

which I'm trying to transform into the string:

@"There are other worlds than these"

I'm g开发者_运维问答uessing this is a regex job, but I'm very new to Objective C, and have so far had no luck. I would greatly appreciate any help!


I'd use GTMRegex (http://code.google.com/p/google-toolbox-for-mac/), for example:

NSString *promise = @"thereAreOtherWorldsThanThese";
GTMRegex *regex = [GTMRegex regexWithPattern:@"([A-Z])"];
NSLog(@"%@", [[regex stringByReplacingMatchesInString:promise
                     withReplacement:@" \\1"] lowercaseString]);


As for removing the uppercase letters you can simply use lowercaseString on NSString.

But as for inserting spaces just before an uppercase letter, I would agree that it would be a job for a regex, and sadly, my regex fu is rubbish :)


Without using any libraries you can use this NSString category I posted. Just perform lowerCaseString on the string array. How do I convert an NSString from CamelCase to TitleCase, 'playerName' into 'Player Name'?

0

精彩评论

暂无评论...
验证码 换一张
取 消