Objective-c – Remove prefix from NSString

cocoamacosnsstringobjective cstring

How can I delete the prefix "test" from a NSString?
I've tried stringByReplacingOccurrencesOfString: but it's not what I want because it's the prefix that I want to remove not from the other occurrences of the string.

Best Answer

NSString *prefixToRemove = @"test";
NSString *newString = [originalString copy];
if ([originalString hasPrefix:prefixToRemove])
    newString = [originalString substringFromIndex:[prefixToRemove length]];