您的位置:首页 > 其它

字符串与基本数据类型转换

2017-03-26 20:30 204 查看

1.

- (NSUInteger)length;

返回字符串的长度(有多少个文字)

- (unichar)characterAtIndex:(NSUInteger)index;

返回index位置对应的字符

2.字符串和其他数据类型转换

转为基本数据类型

- (double)doubleValue;

- (float)floatValue;

- (int)intValue;

NSString *str1 = @"110";
NSString *str2 = @"10";
int res = str1.intValue + str2.intValue;
NSLog(@"res = %i", res);


NSString *str1 = @"110";
NSString *str2 = @"10.1";
double res = str1.doubleValue + str2.doubleValue;
NSLog(@"res = %f", res);


转为C语言中的字符串

  - (char *)UTF8String;

NSString *str = @"abc";
const char *cStr = [str UTF8String];
NSLog(@"cStr = %s", cStr);


char *cStr = "abc";
NSString *str = [NSString stringWithUTF8String:cStr];
NSLog(@"str = %@", str);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: