一 NSString不可变字符串
1.字符串的创建
//创建字符串常量
NSString *string1 = @"hello";
string1 = @"hello word";
//alloc创建一个字符串
NSString *string2 = [[NSString alloc] initWithString:@"hello"];
NSString *string3 = [[NSString alloc] initWithFormat:@"hello %@",string1];
NSLog(@"string2:%@",string2);
NSLog(@"string3:%@",string3);
2.字符串的拼接
NSString *str1 = @"张三";
NSString *str2 = @"李四";
NSString *str3 = @"王五";
NSInteger a = 10;
NSString *string4 = [[NSString alloc] initWithFormat:@"%@,%@,%@,%ld",str1,str2,str3,a];
NSLog(@"string4:%@",string4);
3.类方法创建字符串
NSString *string5 = [NSString stringWithString:@"这是一个字符串"];
NSString *string6 = [NSString stringWithFormat:@"hello %@",string5];
NSLog(@"%@",string6);
4.字符串的比较
NSString *s1 = @"zhangsan";
NSString *s2 = @"zhangsan";
//--------------比较指针--------------
if (s1 == s2) {
NSLog(@"指针地址一样");
}
//--------------比较内容--------------
//isEqualToString 区分大小写的
if ([s1 isEqualToString:s2]) {
NSLog(@"字符串内容一样");
}
5.使用isEqualToString
NSString *string7 = [[NSString alloc] initWithFormat:@"abc%@",@"def"];
NSString *string8 = [[NSString alloc] initWithFormat:@"abc%@",@"def"];
if ([string7 isEqualToString:string8]) {
NSLog(@"string7和string8的内容一样");
}
if (string7 == string8) {
NSLog(@"string7和string8的指针一样");
}
6.比较大小
NSString *string9 = @"changXiAngwEilai";
NSString *string10 = @"Changxiangweilai";
//compare 区分大小写的比较
NSComparisonResult result = [string9 compare:string10];
if (result == NSOrderedDescending) {
NSLog(@"这是降序");
}else {
NSLog(@"这是升序");
}
//caseInsensitiveCompare:不区分大小写的比较
result = [string9 caseInsensitiveCompare:string10];
if (result == NSOrderedSame) {
NSLog(@"不区分大小写相同");
}
7.判断字符串的开头
NSString *string11 = @"changxiangweilai";
if ([string11 hasPrefix:@"chang"]) {
NSLog(@"string11是以chang开头");
}
8.判断字符串的结尾
if ([string11 hasSuffix:@"lai"]) {
NSLog(@"string11是以lai结尾");
}
9.字符串转换
NSString *string12 = @"heLlO WoRd";
//将字符串转换成小写lowercaseString
NSLog(@"小写:%@",string12.lowercaseString);
//将字符串转换成大写uppercaseString
NSLog(@"大写:%@",string12.uppercaseString);
//首字母改成大写,其他小写capitalizedString
NSLog(@"首字母大写:%@",string12.capitalizedString);
10.字符串和基本数据类型转换
NSString *string13 = @"12";
//string-> int
NSInteger intValue = [string13 integerValue];
NSLog(@"intValue:%ld",intValue);
NSString *string14 = @"3.4";
float floatValue = [string14 floatValue];
NSLog(@"floatValue:%f",floatValue);
NSString *string15 = @"12";
[string15 boolValue];
11.字符串的长度
NSString *string15 = @"abcdefg";
NSUInteger length = string15.length;
NSLog(@"length:%ld",length);
12.根据下标获取字符
unichar c = [string15 characterAtIndex:3];
NSLog(@"c:%c",c);
13.字符串的截取
NSString *string16 = @"www.baidu.com";
//1.截取到某一个指定的位置 substringToIndex (不包含当前的下标)
// 截取出www
NSString *subStr1 = [string16 substringToIndex:3];
NSLog(@"subStr1:%@",subStr1);
//2.从指定位置截取 substringFromIndex (包含当前下标)
//截取出com
NSString *subStr2 = [string16 substringFromIndex:10];
NSLog(@"subStr2:%@",subStr2);
//3.从某一个指定的截取指定长度
//baidu
NSRange rg = {4,5};
NSString *subStr3 = [string16 substringWithRange:rg];
NSLog(@"subStr3:%@",subStr3);
14.字符串的追加
NSString *string17 = @"Andriod";
//在Andriod后面追加 iOS
NSString *appendStr1 = [string17 stringByAppendingString:@" iOS"];
NSLog(@"appendStr1:%@",appendStr1);
NSString *appendStr2 = [string17 stringByAppendingFormat:@" %@%d",@"iOS",9];
NSLog(@"appendStr2:%@",appendStr2);
15.字符串的检索
NSString *string18 = @"12345678@qq.com";
//检索字符串中的qq.com
NSRange rg1 = [string18 rangeOfString:@"qqcom"];
//返回的结果:如果找到了,则返回字符串的信息;如果找不带,则location会返回一个NSNotFound
NSLog(@"location:%ld length:%ld",rg1.location,rg1.length);
if (rg1.location == NSNotFound) {
NSLog(@"找不到");
}
二. NSMutableString(可变字符串)
1.可变字符串的创建
//NSMutableString *mutStr1 = @"abc"; //不能这么写是常量@“abc”
NSMutableString *mutStr1 = [[NSMutableString alloc] initWithString:@"字符串"];
2.插入数据
[mutStr1 insertString:@"可变" atIndex:0];
3.追加
[mutStr1 appendString:@"对象哈哈"];
4.删除
//删除“哈哈”
NSRange rg2 = [mutStr1 rangeOfString:@"哈哈"];
[mutStr1 deleteCharactersInRange:rg2];
5.替换
//(1)检索需要替换的字符串
NSRange rg4 = [mutStr1 rangeOfString:@"字符串"];
//(2)替换
[mutStr1 replaceCharactersInRange:rg4 withString:@"数组"];
NSLog(@"mutStr1:%@",mutStr1);
6.按照某个字符将字符串截取成若干个字符串
NSString *str = @"this is string";
NSArray *arr = [str componentsSeparatedByString:@" "];
NSLog(@"arr = %@",arr);