NSString *str1 = @"This is string A";
NSString *str2 = @"This is string B";
NSString *res;
NSRange subRange;// 注意不是对象变量,是结构变量
res = [str1 substringToIndex:3];//substringToIndex 索引数是0开始,所以提取的是字符 0,1,2,如果索引数无效,会获得 Range(范围) or index out of bounds 的出错信息
NSLog(@"First 3 chars of str1:%@",res);
res = [str1 substringFromIndex:5];// 输出从索引字符开始到字符串结尾
NSLog(@"Chars from index 5 of str1 :%@", res);
res = [[str1 substringFromIndex:8] substringToIndex:6];//复合用法
NSLog(@"Chars from index 8 throughe 13 :%@", res);
res = [str1 substringWithRange:NSMakeRange(8, 6)];//接受范围函数
NSLog(@"Chars from index 8 throughe 13 :%@ ", res);
subRange = [str1 rangeOfString:@"string A"];//在一个字符串中查找另一个字符串的方法
NSLog(@"string is at index %lu, length is %lu", (unsigned long)subRange.location,(unsigned long)subRange.length);
subRange = [str1 rangeOfString:@"string B"];
if (subRange.location == NSNotFound)
NSLog(@"string not found");
else
NSLog(@"String is at index %lu, length is %lu", (unsigned long)subRange.location, (unsigned
long)subRange.length);