views:

152

answers:

2

Hi,

I wanted to know if there is any (easy) method to find the order of a given array of substrings in an NSString?

I have a large block of text and a few substrings. I'm only interested in the order that the substrings first appear in the text.

So if the text was "can you tell me you are working late if you can" and the substrings were "can", "if" and "you", I'd want an array that contains the substrings in the following order: can, you, if.

It'd be fairly lengthy to parse the text my self so I'm wondering if there is free functionality for this?

Thanks

A: 

Well the simple way would be to do it and see how it comes out. It would be unlikely that the parsing direction would change randomly for no reason.

So what I am saying is, just run it, make a note of how it parses, and use that as a guideline. Test when necessary.

Jasconius
+3  A: 

See the -rangeOfSubstring: method. Find where the words you care about are, and then compare the starting value of their ranges.

NSResponder
Caution: Depending on the substrings you're looking for, you may need to avoid overlapping ranges. It would probably be a good idea to cover this case with unit tests anyway, so that you can change the list of substrings later (or at runtime) without falling into the trap.
Peter Hosey
That worked nicely. And I'm keeping Peter's advice in mind. So thanks, both of you!
cocoaholic