次の文字列があるとします。
Mary had a little lamb, she also had a little sheep.
had
私の目標は、期間の前後のすべての単語を抽出することです。(この場合a little sheep
)。
私はこの方法を試しました:
- (NSInteger)indexOf:(NSString*)substring from:(NSInteger)starts {
NSRange r;
r.location = starts;
r.length = [self length] - r.location;
NSRange index = [self rangeOfString:substring options:NSLiteralSearch range:r];
if (index.location == NSNotFound) {
return -1;
}
return index.location + index.length;
}
のように:
NSInteger sheepSpot = [string indexOf:@"had" from:23];
// I know that I want to grab everything after the index of sheepSpot but before the period.
// Suppose now that I have an arbitrary number of periods in the sentence, how can I extract the above text without getting the wrong thing?