输出NSRange类型的数据

时间:2023-03-26 20:25:02

如果你想知道字符串内的某处是否包含其他字符串,使用rangeOfString: 方法

-(NSRange)rangeOfString :(NSString *)aString;

         NSString *num = @"sdhfdsjkljg";

         NSRange a =[num rangeOfString:@"hf"];
NSLog(@"%@",NSStringFromRange(a));
//输出{2, 2}

NSStringFromRange   :Returns a string representation of a range

        NSString *str = @"hello world ,I'm coming";
NSRange range = [str rangeOfString:@"world"];
NSLog(@"location:%ld,length:%ld",range.location,range.length);