1、创见常量字符串,注意使用“@”符号
A、 NSString *str = @"I am amos!";//特殊用法 B、常规用法 NSString * str = [NSString new]; str = @"I am amos!";
C、格式化创建字符串
NSString * str = [NSString stringWithFormat:@"%d",4]; 2、全是英文字母时,lenght方法与strlen()、sizef的区别 - <font size="4"> NSString * str = @"I am amos!";
- char str2[] = "I am amos!";
- printf("[str length]= %ld\n",[str length]);
- printf("strlen(str2)= %ld\n",strlen(str2));
- printf("sizeof(str2) = %ld\n",sizeof(str2));</font>
复制代码打印结果: [str length]= 10 strlen(str2)= 10 sizeof(str2) = 11 分析结果:
lenght方法与strlen()都不包含‘\0’,而sizeof包含'\0'
3、在有中文的情况下比较 - NSString * str = @"I am叶良辰!";
- char str2[] = "I am叶良辰!";
- printf("[str length]= %ld\n",[str length]);
- printf("strlen(str2)= %ld\n",strlen(str2));
- printf("sizeof(str2) = %ld\n",sizeof(str2));
复制代码打印结果: [str length]= 8 strlen(str2)= 14
sizeof(str2) = 15
|