OC 中一种常见的把其他类型转为字符串的方法:stringWithFormat
NSString *height;
height= [NSString stringWithFormat:@"Your height is %d feet, %dinches.",5,11];
NSLog(@"%@",height);
输出结果:
2013-04-12 10:30:47.742String[2161:303] Hello, World!
2013-04-12 10:30:47.744String[2161:303] Your height is 5 feet, 11 inches.
输出多个字符的方式(以两个字符为例):
NSString *str;
NSString *str1 =@"123";
NSString *str2 = @"465";
str =[NSString stringWithFormat:@"%@,%@",str1, str2];
NSLog(@"%@",str);
输出结果:
2013-04-12 10:31:48.211 String[2171:303] Hello,World!
2013-04-12 10:31:48.213 String[2171:303]123,465
下面这种方式是错误的:
str =[NSStringstringWithFormat:@"123",@"456"];
NSLog(@"%@",str);
输出结果:
2013-04-12 10:35:34.041 String[2209:303] Hello,World!
2013-04-12 10:35:34.043 String[2209:303]123
str =[NSString stringWithFormat:@"%@,%@",@"123",@"456"];
NSLog(@"%@",str);
输出结果:
2013-04-12 10:42:39.539 String[2229:303] Hello,World!
2013-04-12 10:42:39.541 String[2229:303]123,456
以上文章出处自http://blog.sina.com.cn/s/blog_6b1e4a0601019pib.html,转载请注明原处.