Getting the last word of an NSString

前端 未结 7 1058
滥情空心
滥情空心 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:41

    You could use NSString's function rangeOfSubstring:options: to determine it. For example:

    Search the string for a space, using a backwards search option to start the search from the end of the string.

    NSRange r = [string rangeOfString:@" " options:NSBackwardsSearch];

    This will find the location of the last word of the string. Now just get the string using substringWithRange: For Example:

    NSRange found = NSMakeRange(NSMaxRange(r), string.length - NSMaxRange(r)); NSString *foundString = [string substringWithRange:found];

    Where r is the range from earlier.

    Also be careful to make sure that you check r actually exists. If there is only 1 word in the string, then r will be {NSNotFound, 0}

    Hope I could help!

    Ben

    0 讨论(0)
  • 2020-12-17 00:42

    You can read symbols from the end of the your string and copy them at the 0 index to result string. Whether you read space or comma, result string wil contain the last word

    0 讨论(0)
  • 2020-12-17 00:43

    The most efficient way is likely to start at the end of the string, examine each character to see if it's part of what you define as a word, and then extract the word you want using substringFromIndex: or substringWithRange:.

    0 讨论(0)
  • 2020-12-17 00:52

    Give this a try:

    NSRange range = [someNSStringHere rangeOfString:@" " options:NSBackwardsSearch];
    NSString *result = [someNSStringHere substringFromIndex:range.location+1];
    
    0 讨论(0)
  • 2020-12-17 00:53

    If you want to be super-robust:

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

    (This should also work with non-Roman languages; iOS 4+/OS X 10.6+.)

    Basic explanation:

    -enumerateSubstringsInRage:options:usingBlock: does what it says on the tin: it enumerates substrings, which are defined by what you pass in as the options. NSStringEnumerationByWords says "I want words given to me", and NSStringEnumerationReverse says "start at the end of the string instead of the beginning".

    Since we're starting from the end, the first word given to us in substring will be the last word in the string, so we set lastWord to that, and then set the BOOL pointed to by stop to YES, so the enumeration stops right away.

    lastWord is of course defined as __block so we can set it inside the block and see it outside, and it's initialized to nil so if the string has no words (e.g., if it's empty or is all punctuation) we don't crash when we try to use lastWord.

    0 讨论(0)
  • 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];
    
    0 讨论(0)
提交回复
热议问题