Thursday 28 January 2016

Convert String in Camel Case To UnderScore regex: using NSString with NSRegularExpression

+(NSString*)camelCaseToUnderScore:(NSString*)camelCaseString
{
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?<=[a-z])([A-Z])|([A-Z])(?=[a-z])" options:0 error:nil];

    NSString *underscoreString = [[regex stringByReplacingMatchesInString: camelCaseString options:0 range:NSMakeRange(0, camelCaseString.length) withTemplate:@"_$1$2"] lowercaseString];
    NSLog(@"%@", underscoreString);
    return underscoreString;

}