How to split NSString at uppercase letters

In course of developing one of the applications, I came across a requirement where I had to convert controller names into readable user understandable names. Though I have named controllers by their relevancy, still I wanted to make it look good to a general user as well. And fortunately, StackOverflow came to my rescue.

I got this piece of code which perfectly added space to the names of controller classes and then removed ‘Controller’ word from it thus making it perfectly readable and easily understandable.

NSString *string = NSStringFromClass([self class]);
NSRegularExpression *regexp = [NSRegularExpression regularExpressionWithPattern:@"([a-z])([A-Z])" options:0 error:NULL];
NSString *newString = [[regexp stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, string.length) withTemplate:@"$1 $2"] stringByReplacingOccurrencesOfString:@" Controller" withString:@""] ];

Hope some of my readers will find it helpful. Do leave a comment, if you found it helpful.

Thanks