简体   繁体   中英

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 friend"... 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];

也许看看使用RegexKit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM