rangeOfString的用法


官方描述:

Finds and returns the range of the first occurrence of a given string within the receiver.


例子:

-(NSRange)getRangeWithStr:(NSString *)_str searchStr:(NSString *)_searchStr
{
    return [_str rangeOfString:_searchStr];
}

    NSRange range = [self getRangeWithStr:@"I am lyhuzhu" searchStr:@"hu"];
    NSLog(@"range = %@",NSStringFromRange(range));
    NSRange xrange = [self getRangeWithStr:@"I am lyhuzhu" searchStr:@"vic"];
    NSLog(@"xrange = %@",NSStringFromRange(xrange));

输出结果:

2013-05-08 11:47:37.929 pushTest[3767:c07] range = {7, 2}
2013-05-08 11:47:52.624 pushTest[3767:c07] xrange = {2147483647, 0}


通过下面方式,判断是否查找到


    if (xrange.location == NSNotFound) {
        NSLog(@"yes,found.");
    }
    else{
        NSLog(@"not found");
    }


你可能感兴趣的:(rangeOfString的用法)