Foundation framework study memo
// insert code here...
char s = "hello-c";
NSString str=@"hello-oc";
//OC,C字符串类型转换
//c to oc
NSString str1=[NSString stringWithUTF8String:s];
NSLog(@"str=%@",str1);
//oc to 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=%d0",a,b];
NSLog(@"str5=%@",str5);
//拼接字符串
NSString str6 =[str5 stringByAppendingString:str3];
NSLog(@"str6=%@",str6);
//大小写转换 A -> a
NSString str7 =@"aBCdEF";
NSString str8=[str7 lowercaseString];
NSLog(@"str8=%@",str8);
//a->A
NSString str9=[str7 uppercaseString];
NSLog(@"str9=%@",str9);
//前缀和后缀判断
//前缀判断
NSString str10=@"www.imooc.com";
BOOL hasPreFix =[str10 hasPrefix:@"www."];
if(hasPreFix)
NSLog(@"has matched preFix");
else
NSLog(@"Does not have matched prefix");
//后缀判断
BOOL hasSuffix = [str10 hasSuffix:@".com"];
if(hasSuffix)
NSLog(@"has matched suffix");
else
NSLog(@"Does not have matched suffix");
//判断两个字符串是否相同
NSString str11=@"same";
NSString *str12=@"same";
if([str11 isEqualToString:str12])
NSLog(@"the same");
else
NSLog(@"is not same");
//比较字符串NSComparisonResult
//分割 //按照指定字符分割字符串 NSString *str13=@"a,b,c,d,e,f,g"; NSArray *strArray =[str13 componentsSeparatedByString:@","]; NSLog(@"NSArray-%@",strArray); //按照范围截取字符串 NSRange range = NSMakeRange(1, 5); NSString *str14=[str13 substringWithRange:range]; NSLog(@"str14=%@",str14); //从某一位开始截取后面的字符串 NSString *str15=[str13 substringFromIndex: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 j ab"; //查找指定字符串位置 NSRange range1=[str17 rangeOfString:@"ab"]; NSLog(@"range1.location:%ld range1.length:%ld",range1.location,range1.length); //替换 NSString *str18=@"hello ios,hello imooc"; //替换某一个范围的内容 NSString *str19 = [str18 stringByReplacingCharactersInRange:NSMakeRange(1, 3) withString:@"你好"]; NSLog(@"str19=%@",str19); //用置顶字符串替换元字符串中的字串 //tringByReplacingOccurrencesOfString:被替换的内容 //withString:替换内容 NSString *str20=[str18 stringByReplacingOccurrencesOfString:@"hello" withString:@"aroha"]; NSLog(@"str20=%@",str20); //读取文件 /*文件来源:本地文件 2 网络文件NSURL 路径类*/ NSString *str21=@"www.baidu.com"; //网络路径 NSURL *httpurl =[NSURL URLWithString:str21]; //本地路径 NSURL *fileurl =[NSURL fileURLWithPath:str21]; //读取网络文件 NSString *httpStr=[NSString stringWithContentsOfFile:httpurl encoding:NSUTF8StringEncoding error:nil]; NSLog(@"httpStr=%@",httpStr); //读取本地文件 NSString *fileStr=[NSString stringWithContentsOfFile:@"/Users/jQ/Desktop/testfile.txt" encoding:NSUTF8StringEncoding error:nil]; NSLog(@"fileStr=%@",fileStr); //写入文件 NSString *str22=@"hello hansom guy"; BOOL isOK=[str22 writeToFile:@"/Users/jQ/Desktop/demo.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil]; if(isOK) NSLog(@"str22 is successful output"); else NSLog(@"str22 output is faild");
评论 (0)