Sagiftw Sagiftw - 7 months ago 58
Objective-C Question

Finding a substring in a NSString object

I have an NSString object and I want to make a substring from it, by locating a word.

For example, my string is: "The dog ate the cat", I want the program to locate the word "ate" and make a substring that will be "the cat".

Can someone help me out or give me an example?



NSRange range = [string rangeOfString:@"ate"];
NSString *substring = [[string substringFromIndex:NSMaxRange(range)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];