96

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?

nall
  • 15,899
  • 4
  • 61
  • 65
Debashis
  • 1,025
  • 1
  • 8
  • 10

1 Answers1

287

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

[@"abc def ghi abc def ghi" rangeOfString:@"abc" options:NSBackwardsSearch];
outis
  • 75,655
  • 22
  • 151
  • 221