1 NSString不可变字符串
char *s = @"hello.world";
NSString *str = @"Hello";//字符串格式
//OC,C 字符串的转换
NSString *str1 = [NSString stringWithUTF8String:s];//C ->OC
NSLog(@"str2=@s" ,[str UTF8String]);//OC->C
//创建字符串
NSString *str3 = @"iOS";
NSString *str4 = [[NSString alloc]init];
//格式化字符串
int a =10;
int b= 20;
NSString *str5 = [NSString stringWithFormat:@"a=%d,b=%d,"a,b];
//字符串拼接
NSString *str6 = [str5 stringByAppendingString:str3];
NSLog(@"str6=%@".str6);
//大小写转换
//A->a
NSString *str7 = @"ABCDE";
NSString str8 = [str7 lowercaseString];
//a->A
NSString *str9 = @"abdee";
NSString *str10 = [str9 uppercaseString];
//前缀和后缀的判断
NSString *str11 = @"www.imooc.com";
BOOL hasPerfix = [str11 hasPrefix:@"www."];
if(hasPrefix){
NSLog(@"有对应的前缀");
else{
NSLog(@"没有对应的前缀");
BOOL hasSuffix = @".com";
if (hasSuffix){
NSLog(@"有对应的后缀");
else{
NSLog(@"没有对应的后缀");
//分割字符串
NSString *str12 @"a,b,c,d,e";
NSArray *strArray = [str12 componentsSeparatedByStrin:@","];
for( NSString *str in strArray){
NSLog(@"str = %@",str);//str =a, str = a....
//按照范围截取字符串
NSRange range =NSMakeRange(1,5);
NSString *str13 = [str12 substringWithRange:range];
//从某一位截取后面的字符串
NSString *str14 = [str12 substringFormIndex:2];//从第二位开始截取后面的
NSString *str15 = [str12 substringToindex:4];//从开头截取的第四位
//将字符串拆分为每一个字符
for (int i=0; i<[str12 length];i++)
{ NSLog(@"%c",[str12 characterAtIndex:i]);
//查找
NSString *str15 = @"ab, ce ,ef, gh";
//查找指定字符串的位置
NSRange range1 =[str15 rangeofStrin:@"ab"];
NSlog(@"range1.location:%ld,range1.lengt;h",range1.location,range1.length);
//字符串的替换
NSString *str 16 = @"hello,ios,word";
NSString *str17 = [str16 stringByReplacingCharctersInRange:NSMakeRange(0,5) withString:@"你好"];
NSLog(@"str17=%@",str17);
//用指定字符串替换原字符串中的字符
// stringByReplacingOccurencesofString-源字符串中要被替换的内容
NSString *str20 = [str16 stringByReplacingOccurrencesofString:@"hello"withString:@"你好"];
//读取文件
//文件来源 1 :本地文件,2 网络文件
//路径
NSString *strg = @www.baidu.com;
//网络路径
NSURL *httpURL = [NSURL URLWithString:strg];
//本地路径
NSURL*fileURL=[NSURL fileURLWithPath: strg];
//读取网络文件
NSString *httpStr = [NSString stringWithContentsofURL:httpURL encoding:USUTF8StringEncoding error:nil];
//读取本地文件
NSString *fileStr = [NSString stringWithContentsoffile: @"/USers/Visitor/Desktop/test.txt" encoding:NSUTF8StringEncoding error:nil];
//写入文件
NSString *strg1 = @"Hello,Visitor";
[strg1 writeTofile:@"USers/Visitor/Desk/demo.txt" atomically:YES encoding:NSUTF8StringEncoding: error :nil];
//可变字符串 (是字符串的子类)
NSMutableString *str = [[NSMutableString alloc]initWithCapacity:10];
[str1 setString:@"hello"];
//追加字符串
[str appendSting:@" word"];
[str appendFormat:@"-%@".a];
//替换字符串
NSRange range = [str rangeofString:@"word"];
[str replaceCharactersInRange:range withString:@"ios"];
//插入字符串
[str insertString:@"A" atIndex:6];
//删除字符串
NSRange range1 = [str rangeofString:ios];
[str deleteCharactersINRange:range];