我有一个字符串!
NSString *myString=[NSString stringWithFormat:@"This is my lovely string"];
我想做的是:
使用...
将剩余字符串子串到新字符串[myString substringFromIndex:leSpace]
......我希望我解释得很好。请帮助,你能写一个片段或什么来帮助我完成这项任务吗?
答案 0 :(得分:51)
- (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask range:(NSRange)aRange
对于选项,请使用:NSBackwardsSearch
NSRange range = [myString rangeOfString:@" " options:NSBackwardsSearch range:NSMakeRange(0, 11)];
示例:
NSString *myString=[NSString stringWithFormat:@"This is my lovely string"];
NSRange range = [myString rangeOfString:@" " options:NSBackwardsSearch range:NSMakeRange(0, 11)];
NSLog(@"range.location: %lu", range.location);
NSString *substring = [myString substringFromIndex:range.location+1];
NSLog(@"substring: '%@'", substring);
NSLog输出:
range.location: 10
substring: 'lovely string'
当然应该检错range.location
不等于NSNotFound