NSRange 使用...

来源:互联网 发布:网络项目70000元 编辑:程序博客网 时间:2024/06/08 15:14
#import <Foundation/Foundation.h>int main(int argc, const char * argv[]){@autoreleasepool {NSString * str1 = @"123ios123itcast123ios111ios";NSString * str2 = @"ios";NSRange range = [str1 rangeOfString:str2];NSRange temp; // 只能创建一个 range 再赋值 , 再代入参数列表里吗? 有没有直接写法? 有// 方法1://temp.location = 5;//temp.length = 22;// 方法2://        temp = (NSRange){5,22};// 方法3://NSRange temp = { 5, 22 };// 方法4://NSRange temp = {.location = 5, .length = 22 };// 方法5: OC新增的方法, 上面的是传统C的赋值方式temp = NSMakeRange(5, 22);NSRange range2 = [str1 rangeOfString:str2 options:NSLiteralSearch range:temp];// NSNotFound == NSIntegerMaxif (range.location != NSNotFound) {            //查看结构体方法1:NSLog(@"%lu,%lu", range.location, range.length);            //方法2:NSLog(@"%@", NSStringFromRange(range));}else {NSLog(@"not found ");}//==============================if (range2.location != NSNotFound) {NSLog(@"%lu,%lu", range2.location, range2.length);NSLog(@"%@", NSStringFromRange(range2));}else {NSLog(@"not found ");}}return 0;}

0 0