Search code examples
iphoneobjective-ccocoa-touchnsrange

NSRange How to look for a space OR a return?


Possible Duplicate:
How to find and replace symbols in a string?

Sorry if the following is really obvious, but I'm just starting to work with NSRange and found the docs not really helpful with my question.

I have a string and I would like to look through it, starting from the very end. I would like to determine the range from the end of the string to either (1) the first space OR (2) the first return.

This is what I have so far:

 NSRange range = [myString rangeOfString:@" " options:NSBackwardsSearch];

but how do I tell objective-C something like rangeOfString:@" " or @"/n"?

Thanks for any help or suggestions with this!


Solution

  • You can do something like this:

    NSRange rangeSpace = [myString rangeOfString:@" " options:NSBackwardsSearch];
    NSRange rangeReturn = [myString rangeOfString:@"/n" options:NSBackwardsSearch];
    
        if(rangeSpace.location < rangeReturn.location)
            //use rangeReturn
        else
            //use rangeSpace