在文本字段中的特定字符串之前获取文本字段中的字符串

Getting a string in textfield before a specific string in the textfield

所以我的文本字段包含以下文本。 @"A big Tomato is red."

我想获取"is".

之前的单词

当我输入

  NSString *someString = [[textfield componentsSeparatedByString:@"is"]objectAtIndex:0];

我总是得到 "A big Tomato" 而不仅仅是 "Tomato"。在应用程序中,人们会在 "is" 之前输入内容,因此我需要始终在 "is" 之前获取字符串。如果能得到任何帮助,我将不胜感激。 *警告,

这是一个非常难的问题。

试试这个

 NSString *string = @"A big Tomato is red.";
    if ([string rangeOfString:@"is"].location == NSNotFound) {
      NSLog(@"string does not contain is");
    } else {
      NSLog(@"string contains is!");
    }

试试这个

NSString *str = @"A big tomato is red";
NSArray *arr = [str componentsSeparatedByString:@" "];
int index = [arr indexOfObject:@"is"];
if(index > 1)
    NSString *str_tomato = arr[index-1];
else
    //"is" is the first word of sentence

根据yvesleborg的评论

试试这个,

NSString *value = @"A big Tomato is red.";
NSArray *array = [value componentsSeparatedByString:@" "];
if ([array containsObject:@"is"]) {
    NSInteger index = [array indexOfObject:@"is"];
    if (index != 0) {
        NSString *word = [array objectAtIndex:index - 1];
        NSLog(@"%@", word);
    }
}