Getting the last word of an NSString

前端 未结 7 1080
滥情空心
滥情空心 2020-12-17 00:38

As the title suggests, I would like to get the last word out of an NSString. I thought using this code:

NSArray *listItems = [someNSStringHere componentsSepa         


        
7条回答
  •  北海茫月
    2020-12-17 00:53

    If you wanted to use a regular expression (which can be useful if you want to start getting more complicated in terms of what you're looking for at the end of your string), you could do something like:

    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\S+\\Z" options:0 error:nil];
    NSTextCheckingResult *found = [regex firstMatchInString:inputString options:0 range:NSMakeRange(0, [inputString length])];
    
    if (found.range.location != NSNotFound)
        result = [inputString substringWithRange:found.range];
    

提交回复
热议问题