NSString:不可变字符串
NSMutableString:可变字符串
//c语言中的字符串
char *s = "Hello";
NSString *str = @"Hello";
//OC , C字符串类型转换
//C -> OC
NSString *str1 = [NSString stringWithUTF8String:s];
NSLog(@"str1 = %@",str1);
//OC->C
NSLog(@"str2 = %s",[str UTF8String]);
//创建字符串
NSString *str3 = @"iOS";
NSString *str4 = [[NSString alloc]init];
str4 = @"iOS";
//* 格式化字符串 *
int a = 10;
int b = 20;
NSString *str5 = [NSString stringWithFormat:@"a = %d b = %d",a,b];
NSLog(@"str5 = %@",str5);
//字符串拼接
NSString *str6 = [str5 stringByAppendingString:str3];
NSLog(@"str6 = %@",str6);
//大小写转换
//A
//转化小写
NSString *str7 = @"aBcDEf";
NSString *str8 = [str7 lowercaseString];
NSLog(@"str8 = %@",str8);
//转换大写9
NSString *str9 = [str7 uppercaseString]j;
NSLog(@"str9 = %@",str9);
//前缀与后缀的判断
NSString *str10 = @www.imooc.com;
//判断前缀
BOOL hasPreFix = [str10 hasPrefix:@"www."];
if(hasPreFix){
NSLog(@"有对应前缀");
}
else{
NSLog(@"没有对应前缀");}
//判断后缀
BOOL *hasSuffix = [str10 hasSuffix:@".com"];
if(hasSuffix){
NSLog(@"有对应后缀");
}
else{
NSLog(@"没有对应后缀");
}
//判断两个字符串是否相同
NSString *str11 = @"hello";
NSString *str12 = @"hello";
if([str11 isEqualToString str12]){
NSLog(@"两个字符串一致");
}else{
NSLog(@"两个字符串不一致");
}
//比较字符串
NSComparisonResult
//分割
//按照指定字符分割字符串
NSString *str13 = @"a,b,c,d,e,f,g";
NSArray *strArray = [str13 componentsSeparatedByString:@","];
for(NSString *str in strArray){
NSLog(@"str = %@",str);
}
//按照范围截取字符串
NSRange range = NSMakeRange(1,5);
NSString *str14 = [str13 substringWithRange:range];
//NSString *str14 = [str13 substringWithRange:NSMakeRange(1,5)];
NSLog(@"str14 = %@",str14);
//从某一位开始截取后面的字符串(包含这一位)
NSString *str15 = [str13 substringFormIndex:2];
NSLog(@"str15 = %@",str15);
//从开头截取到某一位
NSString *str16 = [str13 substringToIndex:7];
NSLog(@"str16 = %@",str16);
//将字符串拆分为每一个字符
for(int i = 0;i < [str13 length];i++){
NSLog(@"%c",[str13 characterAtIndex:i]);
}
//查找
NSString *str17 = @"ab cd ef gh ij ab";
//查找指定字符串的位置
NSRange range1 = [str17 rangeOfString:@"ab"];
NSLog(@"range1.location:%ld range1.length:%ld",range.location,range1.length);
//替换
NSString &str18 = @"Hello iOS ,Hello imooc";
//替换某一个范围的内容
NSString *str19 = [str18 stringByReplacingCharactersInRange:NAMakeRange(0,5) withString:@"你好"];
NSLog(@"str19 = %@",str19);
//用指定字符串题号源字符串中的字符串
/*
stringByReplacingOccurrencesOfString - 源字符串中要被替换的内容
withString - 替换的字符串
*/
NSString *str20 = [str18 stringByReplacingOccurrencesOfString:@"Hello" withString:@"你好"];
NSLog(@"str20 = %@",str20);
//读取文件
//文件来源:1.本地文件. 2.网络文件
//路径类
NSString *str21 = @"www.baidu.com";
//网络路径
NSURL *httpURL = [NSURL URLWithString:str21];
//本地路径
NSURL *fileURL = [NSURL fileURLWithPath:str21];
//读取网络文件
NSString *httpStr = [NSString stringWithContentsOfURL:httpURL encoding:NSUTF8StringEncoding error:nil];
NSLog(@"httpStr = %@",httpStr);
//读取本地文件
NSString *fileStr = [NSString stringWithContentsOfFile:@"/Users/Visitor/Desktop/text.txt " encoding :NSUTR8StringEncoding error:nil];
NSLog(@"fileStr = %@",fileStr);
//写入文件
NSString *str22 = @"Hello Visitor";
[str22 writeToFile:@"/Users/Visitor/Desktop/demo.txt" atomically: YES encoding:NSUTF8String error:nil];
//可变字符串是字符串的子类
NSMutableString *str = [[NSMutableString alloc]initWithCapacity:10];
[str setString:@"Hello"];
//1.追加字符串
[str appendString:@" world"];
NSLog(@"str = %@",str);
int a = 10;
[str appendingFormat:@"-%d",a];
NSLog(@"str = %@",str);
//2.替换字符串
NSRange range = [str rangeOfString:@"world"];
[str replaceCharactersInRange:range withString:@"iOS"];
//删除字符串
NSRange range1 = [str rangeOfString:@"AiOS"];
[str deleteCharactersInRange:range1];
NSLog(@"str = %@",str);
NSString 和 NSMutaleString的使用与注意
(一)6种创建字符串的形式
//最简单快速的创建方式
NSString *s1 = @"yangyong";
//NSString *s2 = [[NSString alloc] initWithString:@"rose"];
//使用格式
NSString *s3 = [[NSString alloc] initWithFormat:@"my age is %d",10];
//反过来OC字符串转变成C字符串
const char *cs = [s4 UTF8String];
//从文件读取信息到字符串
/NSUTF8StringENcoding 用到中文就可以用这种编码
NSString *s5 = [[NSString alloc]initWithContentOfFile:@"/Users/apple/Desktop/yangyngtest.txy" encoding:NSUTF8StringEncoding error :nil];
//根据资源路径读取内容到字符串
NSURL *url = [[NSURL alloc]initWithString@:@"filr:///Users/apple/Desktop/1.txt"];//这里有三个斜杠
NSURL *url = [NSURL fileURLWithPath:@"/Users/apple/Desktop/yangyongtest2.txt"];//这里已经说明,所以不需要再包含协议头
//打印内容
NSString *s6 = [[NSString alloc]initWithCotentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSLog(@"s6 = \n%@",s6);
(二)使用注意
(1)字符串的导入导出
//把字符串写到文件,若这个文件不存在,则创建一个
[@"yangyong \n gaoding" writeToFile:@"/Users/apple/Desktop/yangyong.txt" atomically :YES encoding: NSUTF8StringEncoding error:nil];
//注意这里如果换行对的话可以使用\n的个数来测试代码量
//把字符串导入到资源位置
NSString *str = @"4234534yangyong";
NSURL *url = [NSURL fileURLWithPath@:"/Users/apple/Desktop/yangyong2.txt"];
[str writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:nil];
//这里的atomically后面可以是YES 和NO,通常使用YES,这样更安全,若中途写入失败,则不再创建文件