Getting the last word NSString

As the name suggests, I would like to get the last word from NSString. I thought using this code:

NSArray *listItems = [someNSStringHere componentsSeparatedByString:@" "]; NSString *lastWordString = [NSString stringWithFormat:@"%@", listItems.lastObject]; anotherNSStringHere = lastWordString; 

But I think that NSArray will take some time to load if it is large (and large) and it does not recognize a comma separated word.

Thanks for the help!

+6
source share
7 answers

If you want to be super-reliable:

 __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 +.)

The main explanation:

-enumerateSubstringsInRage:options:usingBlock: does what it says about the gesture: it lists the substrings that are determined by what you pass as parameters. NSStringEnumerationByWords says: “I need the words given to me,” and NSStringEnumerationReverse says “start at the end of the line, not at the beginning”.

Since we start from the end, the first word given to us in substring will be the last word in the string, so we set lastWord for it, and then set the BOOL pointed to by stop in YES, so the listing stops right away.

lastWord , of course, is defined as __block , so we can set it inside the block and see it outside and initialize to nil , so if the line has no words (for example, if it is empty or all punctuation is used), we will not fail when we try to use lastWord .

+27
source

Try:

 NSRange range = [someNSStringHere rangeOfString:@" " options:NSBackwardsSearch]; NSString *result = [someNSStringHere substringFromIndex:range.location+1]; 
+11
source

This works fine, as it also recognizes characters like @ and #, which enumerateSubstringsInRange: does not.

 NSCharacterSet *charSet = [NSCharacterSet whitespaceAndNewlineCharacterSet]; NSArray *components = [someString componentsSeparatedByCharactersInSet:charSet]; NSString *lastWord = components.lastObject; 
+3
source

If you want to use a regex (which can be useful if you want to get complicated in terms of what you are looking at the end of the line), you can 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]; 
+2
source

The most efficient way will probably start at the end of the line, check each character to see if it is part of what you define as a word, and then extract the desired word with substringFromIndex: or substringWithRange:

+1
source

You can read the characters from the end of the line and copy them at index 0 to the result line. If you read a space or a comma, the wil result line contains the last word

0
source

You can use the NSString rangeOfSubstring:options: function to define it. For instance:

Find the line for space using the reverse search option to start the search from the end of the line.

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

This will find the location of the last word of the string. Now just enter the string using substringWithRange: 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 you check r . If there is only 1 word in a line, then r will be {NSNotFound, 0}

Hope I can help!

Ben

0
source

Source: https://habr.com/ru/post/918195/


All Articles