Предположим, у меня есть следующая строка:Обнаружение специфического фрагмента строки в NSString
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?
что бы результат для строки, как это '@«была Мэри была маленькая овечка, она также имела у littlhade овец.»;' –