Objective-C – Substring a NSString

There are 3 methods to substring a NSString.

NSLog([@"1234567890" substringFromIndex:4]);
NSLog([@"1234567890" substringToIndex:6]);
NSLog([@"1234567890" substringWithRange:NSMakeRange(3, 5)]);



2010-07-15 12:39:43.712 Test[29478:20b] 567890
2010-07-15 12:39:43.716 Test[29478:20b] 123456
2010-07-15 12:39:43.719 Test[29478:20b] 45678


Done =)

Reference: NSRange and NSString Objects

21 thoughts on “Objective-C – Substring a NSString”

  1. is there a way to use this with character and not integer

    NSLog([@”1234567890″ substringWithRange:NSMakeRange(@”string”>”, @””)]);

    ex: zfczf will become


  2. NSLog([message substringWithRange:NSMakeRange(@”string”>”, @””)]);

    sorry it will be something like that 😀


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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