开发者

Find words between 2 words?

Using Objective-C on my iPhone, is there a built-in method for NSString that finds a string appearing BETWEEN 2 other stings?

Search "my dog is my cat's best frien开发者_如何学编程d"... and return everything between "dog" and "best".

Or will I have to write my own? Any good ideas on where to start?

Thanks.


You can use substringWithRange:

    NSString *original = @"my dog is my cat's best friend";
    NSString *start = @"dog";
    NSString *end = @"best";
    NSRange startRange = [original rangeOfString:start];
    NSRange endRange = [original rangeOfString:end];
    NSRange range = NSMakeRange((startRange.location+[start length]), (endRange.location-startRange.location-[start length]));

    NSString *between = [original substringWithRange:range];


Perhaps take a look into using RegexKit.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜