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 *str12 = @"410324198510282811"; NSString *str13 = @"19851028"; NSComparisonResult result1 = [str12 compare:str13 options:NSCaseInsensitiveSearch range:NSMakeRange(6,8)]; //options 选项 range 类别 NSLog(@"result1 >>> %ld", result1);
4、字符串拼接
NSString *str14 = @"hello"; NSString *str15 = [str14 stringByAppendingString:@"lisi"];//append 附加,添加 NSString *str16 = [@"/Users/nidediannaomingzi" stringByAppendingPathComponent:@"Desktop"];
5.字符串查找
NSString *str17 = @"nice nice nice"; if ([str17 containsString:@"nnnn"]) { NSLog(@"jin lai le"); }//contains 包含,容纳 NSString *str18 = @"410000xxxxxxxxxx"; if ([str18 hasPrefix:@"41"]) { NSLog(@"shi de "); }// 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, yangyang"; NSLog(@"subString >>> %@", [str21 substringFromIndex:6]);//sub 词根是‘子’ 从第六个字符截取 NSLog(@"subString >>> %@", [str21 substringToIndex:6]); // 截取六个字符 NSLog(@"subString >>> %@", [str21 substringWithRange:NSMakeRange(3, 5)]); //字定义截取范围
//截取?之后的 NSRange sizeRange= [presaleGuideURL rangeOfString:@"?"]; NSString *whatStr1 = [presaleGuideURL substringFromIndex:sizeRange.location+1]; NSLog(@"-->%@",whatStr1);//model=product&action=guidelines&goods_id=138206 //截取到? NSString *whatStr2 = [presaleGuideURL substringToIndex:sizeRange.location]; NSLog(@"-->%@",whatStr2);//http://rwandroid.shetest.cn/index.php //从?之后截取5位 NSRange myRange = [presaleGuideURL rangeOfString:@"?"]; myRange.length = 5; NSString *whatStr3 = [presaleGuideURL substringWithRange:myRange];//?mode NSLog(@"-->%@",whatStr3);
7、字符串大小写转换
NSString *enString = @"In our life, some love was doomed to no end, it is only responsible for the meeting."; NSLog(@"全大写------>%@", [enString uppercaseString]); NSLog(@"全小写------>%@", [enString lowercaseString]); NSLog(@"首字母大写--->%@", [enString capitalizedString]); 全大写------>IN OUR LIFE, SOME LOVE WAS DOOMED TO NO END, IT IS ONLY RESPONSIBLE FOR THE MEETING. 全小写------>in our life, some love was doomed to no end, it is only responsible for the meeting. 首字母大写--->In Our Life, Some Love Was Doomed To No End, It Is Only Responsible For The Meeting. // 字母变换大小写时,中文字符被忽略,不会报错 NSString *enChString = @"ps: 在我们一生中,有的爱情本来就注定没有结局,它只负责相遇。"; NSLog(@"全大写------>%@", [enChString uppercaseString]); NSLog(@"全小写------>%@", [enChString lowercaseString]); NSLog(@"首字母大写--->%@", [enChString capitalizedString]); 全大写------>PS: 在我们一生中,有的爱情本来就注定没有结局,它只负责相遇。 全小写------>ps: 在我们一生中,有的爱情本来就注定没有结局,它只负责相遇。 首字母大写--->Ps: 在我们一生中,有的爱情本来就注定没有结局,它只负责相遇。
/*********可变字符串*********/
//NSMutableString *mstr = [NSMutableString stringWithCapacity:50];// Capacity容量 //NSMutableString *mstr = [NSMutableString stringWithString:@"haha"]; NSMutableString *mstr = [NSMutableString stringWithFormat:@"haha-%@-", @"xiaosan"];
1、追加
[mstr appendString:@"zhang"]; [mstr appendString:@"san"];
2.删除
[mstr deleteCharactersInRange:NSMakeRange(0, 4)];
3.插入
[mstr insertString:@"haha" atIndex:0];
4.替换
[mstr replaceCharactersInRange:NSMakeRange(5, 4) withString:@"woshitihuan"];//从第五个字符开始用teacher替换4个字符
5.格式化追加
[mstr appendFormat:@"-%@", @"stage1"];
6、字符串赋值
str31 = str30; [str31 appendString: @" 和字符串2"];