#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { //创建字符串常量 NSString *str1=@"char1"; //实例方法创建 NSString *str2=[[NSString alloc]init]; NSString *str4=[[NSString alloc]initWithFormat:@"char4"]; NSString *str5=[[NSString alloc]initWithString:str1]; //类方法创建 NSString *str6=[NSString string]; NSString *str7=[NSString stringWithString:str1]; NSString *str8=[NSString stringWithFormat:@"char8"]; //格式化创建 int num1=3; float num2=4.5; NSString *str9=[[NSString alloc]initWithFormat:@"%f",num2]; NSString *str10=[[NSString alloc]initWithFormat:@"%d,%f",num1,num2]; NSString *str11=[NSString stringWithFormat:@"%d,%f",num1,num2]; //字符串长度 NSUInteger len=[str1 length]; //字符串比较 if([str7 isEqualToString:str8]){ NSLog(@"They are sam"); } else{ NSLog(@"They are different"); } //判断字符串大小 NSComparisonResult result1=[str7 compare:str8]; NSComparisonResult result2=[str7 caseInsensitiveCompare:str8]; //字符串转换 float num3=[str9 floatValue]; //大小写转换 NSString *str12=[[NSString alloc]initWithFormat:@"HelloWorld!"]; NSString *str13=[str12 uppercaseString]; NSString *str14=[str12 lowercaseString]; NSString *str15=[str12 capitalizedString]; //将NSString转换为C语言字符串 const char * str16=[str12 UTF8String]; } return 0; }
原文:http://www.cnblogs.com/wangyang1987/p/5105706.html