1

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!

Community
  • 1
  • 1
n.evermind
  • 11,944
  • 19
  • 78
  • 122
  • Other possibilities with similar answers include: [Replace multiple characters in a string in Cbjective-C](http://stackoverflow.com/questions/713918/) and [Check for unallowed characters in string](http://stackoverflow.com/questions/3605918/) – jscs Jun 12 '11 at 20:42

2 Answers2

2

Check out rangeOfCharactersInSet This sounds like what you're looking for:

NSCharacterSet *charsToFind = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSRange charsRange = [testString rangeOfCharacterFromSet:charsToFind options:NSBackwardsSearch]
Zaky German
  • 14,324
  • 4
  • 25
  • 31
1

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
Cyprian
  • 9,423
  • 4
  • 39
  • 73