#import <Foundation/Foundation.h>
struct Location {
//開始下標(biāo)
NSUInteger begin;
//結(jié)束下標(biāo)
NSUInteger end;
};
typedef struct Location Location;
@interface NSString (Location)
- (Location)rangeOfSubString:(NSString *)str;
@end
#import "NSString+Location.h"
@implementation NSString (Location)
- (Location)rangeOfSubString:(NSString *)str {
//獲取字符串的范圍
NSRange range = [self rangeOfString:str];
Location location = {range.location, (range.location + range.length -1)};
return location;
}
@end
#import <Foundation/Foundation.h>
#import "NSString+Location.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSString *str = @"Patience is bitter, but its fruit is sweet.";
Location location = [str rangeOfSubString:@"bitter"];
//輸出開始下標(biāo) 和結(jié)束下標(biāo)
NSLog(@"begin = %lu, end = %lu", location.begin, location.end);
}
return 0;
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者