0

So my textfield has the following text. @"A big Tomato is red."

I want to get the word before "is".

When I type

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

I always get "A big Tomato" instead of just "Tomato". In the app people will type things before "is" so I need to always get the string before "is". I would appreciate any help I can get. *Warning,

This is a very difficult problem.

1
  • 1
    Why don't you split the result "A big Tomato" by space end take the last entry which will be "Tomato"? Commented Jul 14, 2015 at 12:00

3 Answers 3

3

Try this,

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);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try this

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

Comments

0

try this

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

as per yvesleborg's comment

1 Comment

test this with "is a big tomato"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.