簡體   English   中英

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

[英]Getting a string in textfield before a specific string in the textfield

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

我想在“是”之前得到這個詞

當我打字

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

我總是得到"A big Tomato"而不是"Tomato" 在應用程序中,人們會在"is"之前輸入內容,所以我需要始終在"is"之前獲取字符串。 我很感激我能得到的任何幫助。 *警告,

這是一個非常困難的問題。

嘗試這個,

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 *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的評論

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM