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
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
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
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:
.
Give this a try:
NSRange range = [someNSStringHere rangeOfString:@" " options:NSBackwardsSearch];
NSString *result = [someNSStringHere substringFromIndex:range.location+1];
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
.
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];