今天误把NSRangeFromString当成rangeOfString使用了,发现并不是那么回事,记录一下区别
NSRangeFromString
Returns a range from a textual representation.(翻译过来意思大概是:直接用文本来表示范围)
Discussion
Scans *aString*
for two integers which are used as the location and length values, in that order, to create anNSRange
struct. If *aString*
only contains a single integer, it is used as the location value. If *aString*
does not contain any integers, this function returns an NSRange
struct whose location and length values are both 0.
(扫描aString
作为位置和长度值的两个整数,按顺序创造一个NSRange
。如果aString
只包含一个整数,则使用它作为location值。如果' aString '不包含任何整数,则此函数返回一个' NSRange '结构体,其位置和长度值均为0。)
举例说明:
NSString *str1 = @"abc";
NSRange range = NSRangeFromString(str1);
NSLog(@"\nlocation:%ld length:%ld", range.location, range.length);
//location:0 length:0
NSString *str2 = @"1-10";
range = NSRangeFromString(str2);
NSLog(@"\nlocation:%ld length:%ld", range.location, range.length);
//location:1 length:10
NSString *str3 = @"16";
range = NSRangeFromString(str3);
NSLog(@"\nlocation:%ld length:%ld", range.location, range.length);
//location:16 length:0
NSString *str4 = @"9 100";
range = NSRangeFromString(str4);
NSLog(@"\nlocation:%ld length:%ld", range.location, range.length);
//location:9 length:100
NSString *str5 = @"12 16 25";
range = NSRangeFromString(str5);
NSLog(@"\nlocation:%ld length:%ld", range.location, range.length);
//location:12 length:16
rangeOfString就是常规理解的方式了
NSString *str1 = @"123456789";
NSString *str2 = @"6789";
NSRange range = [str1 rangeOfString:str2];
NSLog(@"\nlocation:%ld length:%ld", range.location, range.length);
//location:5 length:4
表示str2 在str1中的range。
网友评论