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)]);

 

Result

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
    zfczf

    Like

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

    sorry it will be something like that 😀

    Like

Leave a comment

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