// 1 创造字符串
NSString *str1 = @"hello";
NSString *str2 = [NSString string];
NSString *str3 = [NSString stringWithFormat:@"hello, %@", @"冀林"];
NSString *str4 = [NSString stringWithCString:"hello,mingming" encoding:NSUTF8StringEncoding];
NSString *str5 = [NSString stringWithUTF8String:"hello,mingming"];
NSString *str6 = [NSString stringWithContentsOfFile:@"/Users/qingyun/Desktop/test.m" encoding:NSUTF8StringEncoding error:nil];//stringwithconentsOfFIle:encoding:error:
NSLog(@"str1 >>> %@", str1);
NSLog(@"str2 >>> %@", str2);
NSLog(@"str3 >>> %@", str3);
NSLog(@"str4 >>> %@", str4);
NSLog(@"str5 >>> %@", str5);
NSLog(@"str6 >>> %@", str6);
// 2 长度
NSLog(@"str6‘length >>>> %ld", [str6 length]);
// 3 字符串比较
NSString *str7 = @"hello000";
NSString *str8 = [NSString stringWithString:str7];
NSString *str9 = [NSString stringWithFormat:@"HE%@",@"llo00000"];
NSLog(@"str7 >>>>> %p", str7);
NSLog(@"str8 >>>>> %p", str8);
NSLog(@"str9 >>>>> %p", str9);
// str7和str9本质是两个指针,所以直接用==比较值
if (str7 == str9) {
NSLog(@"相等");
} else {
NSLog(@"不相等");
}
// 比较字符串内容是否一样
if ([str7 isEqualToString:str9]) {
NSLog(@"相等");
} else {
NSLog(@"不相等");
}
// NSString *str10 = @"100000";
// NSString *str11 = @"000100000";
// NSComparisonResult result = [str10 compare:str11 options:NSNumericSearch];
// NSLog(@"result >>> %ld", result);
NSString *str12 = @"410324198510282811";
NSString *str13 = @"19851028";
NSComparisonResult result1 = [str12 compare:str13 options:NSCaseInsensitiveSearch range:NSMakeRange(6,8)]; //options 选项 range 类别
NSLog(@"result1 >>> %ld", result1);
// 4.连接字符chuan
NSString *str14 = @"hello";
NSString *str15 = [str14 stringByAppendingString:@"weiliang"];//append 附加,添加
NSString *str16 = [@"/Users/qingyun" stringByAppendingPathComponent:@"Desktop"];//Component 组件,成分,要素
NSLog(@"str15 >>>>> %@", str15);
NSLog(@"str16 >>>>> %@", str16);
// 5.字符串查找
NSString *str17 = @"今天我们学习gcd";
if ([str17 containsString:@"gcd"]) {
NSLog(@"少年,好好学习吧,不要关心你不该关心的东西..");
}//contains 包含,容纳
NSString *str18 = @"410000xxxxxxxxxx";
if ([str18 hasPrefix:@"41"]) {
NSLog(@"是河南人");
}// hasPrefix:方法的功能是判断创建的字符串内容是否以某个前缀开始
NSString *str19 = @"icon.exe";
if ([str19 hasSuffix:@"png"] || [str19 hasSuffix:@"jpg"]) {
NSLog(@"合法头像");
}//hasSuffix:方法的功能是判断创建的字符串内容是否以某个后缀开始
NSString *str20 = @"~/Desktop/test.h";
NSLog(@"pathExtension >>> %@", [str20 pathExtension]);// 文件扩展名 Extension 扩展,扩大
NSLog(@"absolute path >>> %@", [str20 stringByExpandingTildeInPath]); //Expand 展开 tilde波浪号
NSLog(@"absolute path >>> %@", [[str20 stringByExpandingTildeInPath] stringByAbbreviatingWithTildeInPath]); // 合闭波浪号 abbreviate缩略 缩短
// 6 提取子串
NSString *str21 = @"hello, yangxu";
NSLog(@"subString >>> %@", [str21 substringFromIndex:6]);//sub 词根是‘子’ 从第六个字符截取
NSLog(@"subString >>> %@", [str21 substringToIndex:6]); // 截取六个字符
NSLog(@"subString >>> %@", [str21 substringWithRange:NSMakeRange(3, 5)]); //字定义截取范围
/*********可变字符串*********/
//NSMutableString *mstr = [NSMutableString stringWithCapacity:50];// Capacity容量
//NSMutableString *mstr = [NSMutableString stringWithString:@"1506"];
NSMutableString *mstr = [NSMutableString stringWithFormat:@"1506-%@-", @"master"];
// 1追加
[mstr appendString:@"zhang"];
NSLog(@"mstr >>> %@", mstr);
[mstr appendString:@"qinpei"];
NSLog(@"mstr >>> %@", mstr);
// 2.删除
[mstr deleteCharactersInRange:NSMakeRange(0, 4)];
NSLog(@"mstr >>> %@", mstr);
// 3.插入
[mstr insertString:@"1506" atIndex:0];
NSLog(@"mstr >>> %@", mstr);
// 4.替换
[mstr replaceCharactersInRange:NSMakeRange(5, 4) withString:@"teacher"];//从第五个字符开始用teacher替换4个字符
NSLog(@"mstr >>> %@", mstr);
// 5.格式化追加
[mstr appendFormat:@"-%@", @"stage1"];
NSLog(@"mstr >>> %@", mstr);
2015-07-18 10:14:17.013 TestNSString[3033:385566] str1 >>> hello
2015-07-18 10:14:17.014 TestNSString[3033:385566] str2 >>>
2015-07-18 10:14:17.014 TestNSString[3033:385566] str3 >>> hello, 冀林
2015-07-18 10:14:17.014 TestNSString[3033:385566] str4 >>> hello,mingming
2015-07-18 10:14:17.014 TestNSString[3033:385566] str5 >>> hello,mingming
2015-07-18 10:14:17.014 TestNSString[3033:385566] str6 >>> (null)
2015-07-18 10:14:17.014 TestNSString[3033:385566] str6‘length >>>> 0
2015-07-18 10:14:17.014 TestNSString[3033:385566] str7 >>>>> 0x1000021b0
2015-07-18 10:14:17.014 TestNSString[3033:385566] str8 >>>>> 0x1000021b0
2015-07-18 10:14:17.015 TestNSString[3033:385566] str9 >>>>> 0x100300210
2015-07-18 10:14:17.015 TestNSString[3033:385566] 不相等
2015-07-18 10:14:17.015 TestNSString[3033:385566] 不相等
2015-07-18 10:14:17.015 TestNSString[3033:385566] result1 >>> 0
2015-07-18 10:14:17.015 TestNSString[3033:385566] str15 >>>>> helloweiliang
2015-07-18 10:14:17.015 TestNSString[3033:385566] str16 >>>>> /Users/qingyun/Desktop
2015-07-18 10:14:17.015 TestNSString[3033:385566] 少年,好好学习吧,不要关心你不该关心的东西..
2015-07-18 10:14:17.015 TestNSString[3033:385566] 是河南人
2015-07-18 10:14:17.025 TestNSString[3033:385566] pathExtension >>> h
2015-07-18 10:14:17.026 TestNSString[3033:385566] absolute path >>> /Users/qingyun/Desktop/test.h
2015-07-18 10:14:17.026 TestNSString[3033:385566] absolute path >>> ~/Desktop/test.h
2015-07-18 10:14:17.026 TestNSString[3033:385566] subString >>> yangxu
2015-07-18 10:14:17.026 TestNSString[3033:385566] subString >>> hello,
2015-07-18 10:14:17.026 TestNSString[3033:385566] subString >>> lo, y
2015-07-18 10:14:17.026 TestNSString[3033:385566] mstr >>> 1506-master-zhang
2015-07-18 10:14:17.026 TestNSString[3033:385566] mstr >>> 1506-master-zhangqinpei
2015-07-18 10:14:17.026 TestNSString[3033:385566] mstr >>> -master-zhangqinpei
2015-07-18 10:14:17.027 TestNSString[3033:385566] mstr >>> 1506-master-zhangqinpei
2015-07-18 10:14:17.027 TestNSString[3033:385566] mstr >>> 1506-teacherer-zhangqinpei
2015-07-18 10:14:17.027 TestNSString[3033:385566] mstr >>> 1506-teacherer-zhangqinpei-stage1