#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
//字符串常量(常量区)
NSString *string1 = @"字符串常量";
NSLog(@"string1 : %@", string1);
//空字符串
NSString *string2 = [[NSString alloc] init];
NSString *string3 = [NSString string];
NSLog(@"string2 : %@", string2);
NSLog(@"string3 : %@", string3);
//这两个创建出来的是在常量区
NSString *string4 = [[NSString alloc] initWithString:@"123"];
//新版本的SDK已经不推荐使用此方法创建NSString,可以直接常量赋值
//Using 'stringWithString:'with a literal is redundant
NSString *string5 = [NSString stringWithString:@"abc"];
NSLog(@"string4 : %@", string4);
NSLog(@"string5 : %@", string5);
//格式化创建字符串(6.7创建出来的是堆区)
NSString *string6 = [[NSString alloc] initWithFormat:@"%.2f",31.2F];
NSString *string7 = [NSString stringWithFormat:@"%d",690];
NSLog(@"string6 : %@", string6);
NSLog(@"string7 : %@", string7);
//常量区域
NSString *string8 = [[NSString alloc] initWithString:@"bbb"];
NSString *string9 = [[NSString alloc] initWithString:@"bbb"];
//堆区域
NSString *string10 = [[NSString alloc] initWithFormat:@"bbb"];
//比较内容
if ([string8 isEqualToString:string9]) {
NSLog(@"same content");
}else {
NSLog(@"different content");
}
//比较是否为同一个对象
if (string8 == string9) {
NSLog(@"same pointer");
}else {
NSLog(@"different pointer");
}
if (string8 == string10) {
NSLog(@"True! string8 address: %p string10 : %p", string8, string10);
}else {
NSLog(@"False! string8 address: %p string10 : %p", string8, string10);
}
NSString *string11 = [[NSString alloc] initWithFormat:@"E"];
NSString *string12 = [[NSString alloc] initWithFormat:@"d"];
//比较字符串大小(忽略大小写)
NSComparisonResult result = [string11 caseInsensitiveCompare:string12];
//此时会打印1,表示为降序,如果一样会打印0
NSLog(@"compare result : %d", result);
//转换为基本数据类型
NSString *string13 = @"5.98";
NSLog(@"boolValue : %d", [string13 boolValue]);
NSLog(@"floatValue : %f", [string13 floatValue]);
NSLog(@"doubleValue : %f", [string13 doubleValue]);
NSLog(@"intValue : %d", [string13 intValue]);
//字符串转换
NSString *string14 = @"heLlO NsStrinG";
//转换大写
NSLog(@"upper : %@", [string14 uppercaseString]);
//转换小写
NSLog(@"lower : %@", [string14 lowercaseString]);
//首个字母大写
NSLog(@"capitalized : %@", [string14 capitalizedString]);
//转换成数组
NSString *string15 = @"123 abc e21 aa3";
NSArray *array = [string15 componentsSeparatedByString:@" "];
NSLog(@"array : %@", array);
//截取字符串
NSString *string16 = @"123adf";
//从第2个截取到末尾
NSLog(@"subStringFromIndex2 : %@", [string16 substringFromIndex:2]);
//从第开头截取到第4个
NSLog(@"subStringToIndex4 : %@", [string16 substringToIndex:4]);
//范围截取(从第三个开始截取,截取长度为2个)
NSRange range = {3, 2};
NSLog(@"range : %@", [string16 substringWithRange:range]);
//字符串拼接
NSString *appString1 = @"123";
NSString *appString2 = @"abc";
NSLog(@"append string : %@", [appString2 stringByAppendingString:appString1]);
NSString *rangeString1 = @"adfasd367890b2342_af+=234";
NSRange range1 = [rangeString1 rangeOfString:@"67890"];
if(range1.location == NSNotFound) {
NSLog(@"string not found");
}else{
NSLog(@"range1 %@",NSStringFromRange(range1));
}
//可变的字符串
NSMutableString *mutableString1 = [[NSMutableString alloc] initWithString:@"123abc"];
[mutableString1 insertString:@"fff" atIndex:3];
NSLog(@"insertString : %@",mutableString1);
//替换
[mutableString1 replaceCharactersInRange:NSMakeRange(3, 3) withString:@"bbb"];
NSLog(@"replace : %@",mutableString1);
//删除
[mutableString1 deleteCharactersInRange:NSMakeRange(3, 3)];
NSLog(@"delete : %@",mutableString1);
}
return 0;
}
日志如下:
string1 : 字符串常量
string2 :
string3 :
string4 : 123
string5 : abc
string6 : 31.20
string7 : 690
same content
same pointer
False! string8 address: 0x30e0 string10 : 0x52b030
compare result : 1
boolValue : 1
floatValue : 5.980000
doubleValue : 5.980000
intValue : 5
upper : HELLO NSSTRING
lower : hello nsstring
capitalized : Hello Nsstring
array : (
123,
abc,
e21,
aa3
)
subStringFromIndex2 : 3adf
subStringToIndex4 : 123a
range : ad
append string : abc123
range1 {7, 5}
insertString : 123fffabc
replace : 123bbbabc
delete : 123abc