Substrings from NSString :: N-FAQ

Aasim Naseem 11
subString -

Hello iOS geeks …

Here is a small tip for getting a substring from NSString;
NSString class provide the following methods for splitting a string;

– (NSString *)substringFromIndex:(NSUInteger)from;
– (NSString *)substringToIndex:(NSUInteger)to;
– (NSString *)substringWithRange:(NSRange)range;
– (NSRange)rangeOfString:(NSString *)<em>aString;

The following code will demonstrate itself;

NSString testString = @”I love programming in objective-c”;
[testString substringToIndex:5]; // will return I Love
[testString substringFromIndex:7]; // will return pogramming in objective-c
[testString substringWithRange:NSMakeRange(21, 32)]; // will return objective-c

Use this method to check whether the giving string present in the target string or not.
[testString rangeOfString:@”love”]; // Will return an NSRange structure giving the location and length in the receiver of the first occurrence of aString. Returns ]{NSNotFound, 0} if =aString is not found or is empty (@””).

I think self-descriptive code chunk, so no need to write anything more; (:

Happy Development;

This post belongs to N-FAQ category where i post anything i learn new (no matter how it small or big) during my work; N-FAQ = Not-FAQ;

free counters

Comments 11

  1. Hmm is anyone else experiencing problems with the images on this
    blog loading? I’m trying to figure out if its a problem on my end or if it’s
    the blog. Any feed-back would be greatly appreciated.

  2. Hey just wanted to give you a quick heads up.
    The text in your post seem to be running off the screen in Safari.

    I’m not sure if this is a formatting issue or something to do with internet browser
    compatibility but I figured I’d post to let you know.
    The style and design look great though! Hope you get the issue solved soon. Kudos

Leave a Reply

Your email address will not be published.