ios nsrange char from end

ⅰ亾dé卋堺 提交于 2019-12-02 06:12:49

One of the options you can pass to [NSString rangeOfString: options:] is NSBackwardsSearch (which would give you the last item in the string you're looking at).

There's another variant of the rangeOfString method:

- (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask range:(NSRange)aRange

This lets you specify a range to search within, so you could, in a loop find each range and then exclude it from the search range and try again until there were no more matches.

Depending on what you are trying to do, you might have more luck using this:

NSArray *components = [string componentsSeparatedByString:@" - "];

That will split your string into each part that was separated by a @" - " and return them in an array - you can then get the last one using [components lastObject];

And yet another option is to use NSScanner, which is designed for looping through a string, grabbing tokens as you go.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!