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

Ios 在文本字段中的特定字符串之前获取文本字段中的字符串,ios,objective-c,nsstring,Ios,Objective C,Nsstring,因此,我的文本字段有以下文本@“一个大西红柿是红色的。” 我想知道“is”之前的单词 我打字的时候 NSString *someString = [[textfield componentsSeparatedByString:@"is"]objectAtIndex:0]; 我总是得到一个大西红柿,而不仅仅是西红柿。在应用程序中,人们会在“is”之前键入内容,因此我需要始终在“is”之前获取字符串。如果能得到任何帮助,我将不胜感激*警告, 这是一个非常困难的问题 试试这个 NSString

因此,我的文本字段有以下文本<代码>@“一个大西红柿是红色的。”

我想知道“is”之前的单词

我打字的时候

  NSString *someString = [[textfield componentsSeparatedByString:@"is"]objectAtIndex:0];
我总是得到一个大西红柿,而不仅仅是西红柿。在应用程序中,人们会在
“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 *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
根据的评论

试试这个

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
根据的评论

试试这个

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);
    }
}
试试这个

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);
    }
}

你为什么不把结果“一个大西红柿”按空格分开,最后一个条目是“西红柿”?为什么不把结果“一个大西红柿”按空格分开,最后一个条目是“西红柿”?用“是一个大西红柿”来测试这个,用“是一个大西红柿”来测试这个