如何从textfield中获取最后一个单词

时间:2014-07-04 10:06:18

标签: ios xcode uitextfield

我在文本字段中有以下文字

Taj,Restraint,Renovation,Catch

如何从文本字段中单独获取Catch单词。

2 个答案:

答案 0 :(得分:1)

试试这个

 NSString *myString = @"Taj, Restraint, Renovation, Catch" ;   
__block NSString *lastWord = nil;

    [myString enumerateSubstringsInRange:NSMakeRange(0, myString.length) options:(NSStringEnumerationByWords | NSStringEnumerationReverse) usingBlock:^(NSString *substring, NSRange subrange, NSRange enclosingRange, BOOL *stop) {
        lastWord = substring;
        *stop = YES;
    }];

答案 1 :(得分:1)

 NSString *str = @"The Taj, Restraint, Renovation, Catch";
 NSString *lastWord = [[str componentsSeparatedByString:@","] lastObject];