How to remove the first n characters from an NSString

- by

There’s a convenient method for that by the name of substringFromIndex. This example removes the first character:

    NSString *complete = @"12345";
    NSString *cutOff = [complete substringFromIndex:1];
    NSLog(@"%@", cutOff);
    // cutOff is now 2345

substringFromString takes only one parameter which specifies where to start the new string. The first character is 0 based, like in arrays. The parameter can’t be longer than the original string’s length for obvious reasons.

If you enjoy my content, please consider supporting me on Ko-fi. In return you can browse this whole site witout any pesky ads! More details here.

Add your voice!

This site uses Akismet to reduce spam. Learn how your comment data is processed.