NSString的查找字符串位置和截取字符串

查找字符串位置:

NSRange range;
    range = [tmpStr rangeOfString:@"ccd"];
    if (range.location != NSNotFound) {
        NSLog(@"found at location = %d, length = %d",range.location,range.length);
    }else{
        NSLog(@"Not Found");
    }


 
 TestOCC[769:c07] found at location = 2, length = 3

截取字符串:

1.定义一个字符串a, 截取a 的某一个部分,复制给b, b必须是int型

  NSString *a = @"1.2.30";
    int  b= [[a substringWithRange:NSMakeRange(4,2)] intValue]; 
  NSLog(@"a:%@  \n",a  );
  NSLog(@"b:%d",b  );
 
Output :  Q[4005:207]   a:1.2.30  
[4005:207]            b:30


你可能感兴趣的:(IOS开发)