iOS 字符串与其他类型相互转换

it2022-05-05  173

1.字符串转基本数据类型

//int NSString *stringI = @"1314520"; int a = [stringI intValue]; //NSInteger NSInteger b = [stringI integerValue]; float c = [stringI floatValue]; //字符串转NSNumBer NSNumber *A = @([stringI integerValue]); //字符串转数组,互转 NSArray *ary = [string6 componentsSeparatedByString:@","]; string6 = [ary componentsJoinedByString:@","]; //字符串转字典 NSData *jsonData = [stringI dataUsingEncoding:NSUTF8StringEncoding]; NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil]; //字典转字符串 jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil]; NSString *dicstr = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding ]; //字符串转画utf8 NSString *string8 = @"你好苏州"; NSString *UTF8string = [string8 stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; //utf8转字符串 NSString *str0 = [UTF8string stringByRemovingPercentEncoding];

最新回复(0)