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

Leave a Reply

Your email address will not be published. Required fields are marked *