//
// main.m
// 基本数据类型转换成字符串
//
// Created by admin on 15/8/13.
// Copyright (c) 2015年 admin. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc,
const char * argv[]) {
@autoreleasepool {
// 基本数据类型:(int float,double char)
// 1. int --> NSString
int a = 10;
NSString *str1 = [NSString
stringWithFormat:@"%d",a];
NSLog(@"str1 = %@", str1);
// 2. float --> NSString
float f = 3.6;
NSString *str2 = [NSString
stringWithFormat:@"%.1f",f];
NSLog(@"str2 = %@", str2);
// 3. double --> NSString
double d = 9.887;
NSString *str3 = [NSString
stringWithFormat:@"%.4f",d];
NSLog(@"str3 = %@", str3);
// 4. char --> NSString
char c = ‘s‘;
NSString *str4 = [NSString
stringWithFormat:@"%c",c];
NSLog(@"str4 = %@", str4);
// 使用stringWithFormat
将以上字符串拼接
NSString *str5 = [NSString
stringWithFormat:@"%d%.1f%.4f%c",a,f,d,c];
NSLog(@"str5 = %@", str5);
// 注意:
将字符串转换成数据类型的时候,要确定数据正确
// 5. NSString --> int
NSString *str6 = @"100";
int i = [str6 intValue];
NSLog(@"i = %d", i);
// 6. NSString --> folat
NSString *str7 = @"5.6";
float j = [str7 floatValue];
NSLog(@"j = %f", j);
// 7. NSString --> double
NSString *str8 = @"13.1416";
double k = [str8 doubleValue];
NSLog(@"k = %f", k);
// 8. 将字符串中的字母转换成大写
NSString *str9 = @"abcdefg";
NSString *str10 = [str9
uppercaseString];
NSLog(@"str10 = %@", str10);
// 9. 将字符串中的字母转换成小写
NSString *str11 =
@"HAPPYBIRTHDAY";
NSString *str12 = [str11
lowercaseString];
NSLog(@"str12 = %@", str12);
// 10. 将首字母大写
NSString *str13 =
@"dormitoy";
NSString *str14 = [str13
capitalizedString];
NSLog(@"str14 = %@", str14);
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。