#NSString,不可变的字符串--------------------------------#
一旦被创建(创建了内存空间),只可读取,不能被修改和删除内容
NSString *str=@"hello world!";
//(系统自动管理内存)
NSString *str=[[NSString alloc] init];
str=@"ios";
//(手动管理内存)
**C语言转OC语言 **
NSSring *str=[NSString stringWithUTF8Sting:s];
**OC语言转C语言 **
[str UTF8Sring];
格式化字符串输出变量
int a=10;
int b=20;
NSSring *str=[NSString stringWithFormat:@"a=%d b=%d",a,b] ;
NSlog(@"str=%@",str);
拼接字符串
NSString *str1=@"hello"
NSString *str2=@"world"
NSString *str3=[srr1 sringByAppendingSring:str2];
NSLog(@"str3=%@",str3);//"hello world"
字符串大小写转换
NSString *str1=@"HEELO";
NSString *str2=@"world";
NSString *str3=[str1 lowercaseString];
NSString *str4=[str2 uppercaseString];
NSLog(@"str3=%@",str3);//hello
NSLog(@"str4=%@",str4);//WORLD
字符串前缀与后缀判断
//前缀
NNsring *str=@"www.bilibili.com";
BOOL hasPreFix=[str hasPreFix:@"www."];
if(hasPreFix)
NSLog(@"有对应前缀");
else
NSLog(@"没有对应前缀");
//后缀
BOOL hasSuffix=[str hasSuffix:@",com"];
if(hasSuffix)
NSLog(@"有对应后缀");
else
NSLog(@"没有对应后缀");
判断字符串是否相同
NNString *str1="hello";
NNString *str2="hello world";
if([str1 isEqualToString:str2])
NSLog(@"两个字符串相同");
else
NSLog(@"两个字符串不一致");
//NSComparisonResult(比较字符串,返回值为-1,0,1,等价小于等于大于比较) 详细待补完(坑,不区分大小写之类)
字符串分割
指定分割字符串
NSString *str=@"a,b,c,d";
NSArray *strArray=[str componmentsSeparatedByString:@","];
for(NNString *str in strArray)
{
NSLog(@"str=%@",str);
}
//分别输出a , b , c , d
范围截取字符串
NSString *str=@"1,2,3,4,5,6";
NSRange range=NSMakeRange(1,5);
NSString *str1=[str substringWithRange:range];
NSLog(@"str1=%@",str1);//1,2,3
从某一位开始截取后面所有字符串
NSString *str=@"1,2,3,4,5,6";
NSString *str1=[str substringFromIndex:2];
NSLog(@"str1=%@",str1);//1,
从开头截取到某一位字符串
NSString *str=@"1,2,3,4,5,6";
NSString *str1=[str substringToIndex:5];
NSLog(@"str1=%@",str1);//1,2,3
拆分每一个字符
NSString *str=@"1,2,3,";
for(int i=0;i<[str length];i++){
NSLog(@"%c",[str characterAtIndex:i]);
}
//输出 1 , 2 , 3 ,
查找指定字符串位置 (只执行正向查找)
NSString *str=@"ab cd ef g ab";
NsRange range=[str rangeOfString:@"ab"];
NSLog(@"range.location:%ld range.length:%ld",range.location,range.length)
//输出range.location:0 range.length:2
替换范围内字符串
NSString *str=@"hello world, hello you";
NSString *str1=[str stringByReplacingCharactersInRange:(0,5) withString:@"like"]
NSLog(@"%@",str1);//输出 like world, hello you
新的字符串替换掉源字符串的子串
NSString *str=@"hello world, like you";
NSString *str1=[str stringByReplacingOccurencesOfString:@"hello" withString:@"like"]
NSLog(@"str1=%@",str1)//like world, like you
读取文件
文件来源:本地文件or网络文件
//路径类
NSString *str=@"www.juejin.com";
//网络路径
NSURL *httpUrl=[NSURL URLWithString:str]
//本地路径
NUURL *fileUrl=[NSURL fileURLWithPath:str];
//读取网络文件
NSString *httpStr=[NSString stringWithContentsOfURL:httpUrl encoding:NSUTF8StringEncoding error:nil]
NSLog(@"httpStr=%@",httpStr); //输出为(null)(思考状待深究)
//读取本地文件
NSString *fileStr=[NSString stringWithContentOfFile:@"/User/apple/Desktop/text.txt" encoding:NSUTF8StringEncoding error:nil];
NSLog(@"fileStr=%@",fileStr); //fileStr=文本内容
//写入文件
NSString str=@"hello Anna";
BOOL result=[str writeToFile:@"/User/apple/Desktop/demo.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil]
if(reslut)
NSLog(@"写入成功")
else
NSLog(@"写入失败")
#NSMutableString,可变字符串,可用NSString方法,也有特有的方法--------#
可变字符串是字符串的子类
//10(数字)限制字符串长度,提升性能,可以超过10长度
NSMutableString *str=[[NSMutableString alloc] initWithCapacity:10];
[str setString:@"hello"];
//追加字符串
[str appendString:@" world"];
NSLog(@"str=%@",str); //输入hello world
int a=20;
[str appendFormat:@" ,%d",a];
NSLog(@"str=%@",str);//输出 hello world ,20
查找替换字符串
NSMutableString *str=[[NSMutableString alloc] initWithCapacity:10];
[str setString:@"hello world, like you"];
NSRange range=[str rangeOfString:@"you"];
[str replaceCharactersInRange:range withString:@"me"];
NSLog(@"str = %@",str);//输出str = hello world, like me
插入字符串
NSMutableString *str=[[NSMutableString alloc] initWithCapacity:10];
[str setString:@"hello world, like you"];
[str insertString:@" like me",atIndex:21];
NSLog(@"str=%@",str);//str=hello world, like you like me
删除字符串
NSMutableString *str=[[NSMutableString alloc] initWithCapacity:10];
[str setString:@"hello world, like you"];
NSRange range=[str rangeOfString:@" world"];
[str deleteCharactersInRange:range];
NSLog(@"str=%@",str);//输出str=hello, like you