How do I find the last occurrence of a substring in an NSString?

Objective CString

Objective C Problem Overview


How would I get the last occurrence of an NSString within another NSString? For example, in "abc def ghi abc def ghi," I want to find the index of the second "abc," not the first. I know I could do this with a bunch of rangeOfStrings, but is there already a function for that?

Objective C Solutions


Solution 1 - Objective C

Use rangeOfString:options:, including NSBackwardsSearch in the options.

[@"abc def ghi abc def ghi" rangeOfString:@"abc" options:NSBackwardsSearch];

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDebashisView Question on Stackoverflow
Solution 1 - Objective CoutisView Answer on Stackoverflow