NSString *a = @"1.2.30";
int b= [[a substringWithRange:NSMakeRange(4,2)] intValue];
NSLog(@"a:%@ \n",a );
NSLog(@"b:%d",b );
Output : 2011-07-05 11:49:08.170 Q[4005:207] a:1.2.30
2011-07-05 11:49:08.172 Q[4005:207] b:30
解析如下:substringWithRange: 专门截取字符串的一块肉
NSMakeRange(4,2) 从第4个字符开端截取,长度为2个字符,(字符串都是从第0个字符开端数的哦~!)
b = [a intValue]; 将 a 转换为 整数型
b = [a floatValue]; 将 a 转换为 小数型
b = [a boolValue]; 将 a 转换为 布尔型(true / false)
b = [a integerValue]; 将 a 转换为 整数型
b = [a longLongValue]; 将 a 转换为 长整型
2。 字符串截取到第n位 (substringToIndex: n)(第n 位不算再内)
- (void)viewDidLoad
{
NSString *a = @"i like long dress";
NSString *b = [a substringToIndex:4];
NSLog(@"\n b: %@",b);
}
2011-07-11 18:12:40.119 Q[6321:207]
b: i li
3。字符串从第n 位开端截取,直到最后 (substringFromIndex:n)(包含第 n 位)
- (void)viewDidLoad
{
NSString *a = @"i like long dress";
NSString *b = [a substringFromIndex:4];
NSLog(@"\n b: %@",b);
}
2011-07-11 18:15:08.125 Q[6366:207]
b: ke long dress
http://blog.csdn.net/zhangjunjian127/article/details/18262603