//随机产生字符串
char randomString[10];
for (int x=0;x<10;randomString[x++] = (char)('A' + (arc4random_uniform(26))));
NSString *str = [[NSString alloc] initWithBytes:randomString length:10 encoding:NSUTF8StringEncoding];
NSLog(@"str = %@",str);
//NSNumber
NSNumber *number = [[NSNumberalloc]initWithInteger:123];//这个内存管理有误
NSLog(@"这个数为%@",number);//number是一个对象,不是用ld
NSLog(@"引用记数为%ld",[number retainCount]);//引用记数为9223372036854775807
[number release];
NSLog(@"引用记数为%ld",[number retainCount]);
NSInteger num1 = [number integerValue];
NSLog(@"这个num1被integerValue解封装了,值为%ld",num1);
NSNumber *num2 = [[NSNumberalloc] initWithFloat:3.144];
NSLog(@"浮点型的计数为%ld",[num2 retainCount]);
[num2 release];
NSLog(@"浮点型的计数为%ld",[num2 retainCount]);//release后计数也是有误 //浮点型的计数为1152921504606846975
//NSString
char a[]="iphone";
//1.现将一个C语言的字符数组打乱成二进制Byte流,然后再用NSUTF8StringEncoding这个编码方式,重新组合成OC的字符串
NSString *str1 = [[NSStringalloc]initWithBytes:alength:sizeof(a)encoding:NSUTF8StringEncoding];
NSLog(@"重新组成的字符为%@",str1);
/*========2、将一个字符串写入一个文件和从文件中读取一个字符串========*/
//第一个参数是文件的路径
//第二个参数是原子性
//第三个参数编码方式
//第四个参数错误处理
[str1 writeToFile:@"/Users/bin/Desktop/qqq.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSString *str2 = [NSStringstringWithContentsOfFile:@"/Users/bin/Desktop/qqq.txt"encoding:NSUTF8StringEncoding error:nil];
NSLog(@"str2 = %@",str2);
/*========3、创建一个字符串常量=================================*/
NSString *str3=@"ipad";//(字符串常量,存在常量区,不需要内存管理)
NSLog(@"str3=%@",str3);
/*========4、创建一个空的字符串=================================*/
NSString *str4=[[NSString alloc]init];//开辟了内存的
NSString *str5=[NSString string];//便利构造器的方法,不需要手动管理内存
NSString *str6=nil;//只是创建了一个指针而已,没有开辟内存
NSLog(@"str4=%@",str4);
NSLog(@"str5=%@",str5);
NSLog(@"str6=%@",str6); //str6=(null)
//----5,,如果参数是在堆内开辟的,就可以做内存管理,如果参数是常量区的,就不做内存管理
NSString *str7=[[NSString alloc]initWithString:@"333"];
NSString *str8=[NSString stringWithString:str1];
NSLog(@"str7=%@",str7);
NSLog(@"str8=%@",str8);
NSLog(@"str7的引用计数为%ld",[str7 retainCount]);//str7的引用计数为-1
//[str7 release];[str7 release];//开启Enable Zombie Objects 会挂掉
NSLog(@"str7的引用计数为%ld",[str7 retainCount]);//str7的引用计数为-1
NSLog(@"str8的引用计数为%ld",[str8 retainCount]);//str8的引用计数为2
/*========6、创建一个格式化字符串==============================*/
NSString *str9=[[NSString alloc]initWithFormat:@"我想考%d分",100];
NSString *str10=[NSString stringWithFormat:@"我想看%@",@"小时代"];
NSString *str11=[NSString stringWithFormat:@"%@%@%d",@"黄铁军",@"考了",60];
NSLog(@"str9=%@",str9);
NSLog(@"str10=%@",str10);
NSLog(@"str11=%@",str11);
[str9 retain];
NSLog(@"str9的引用计数=%ld",[str9 retainCount]);
[str9 release];
NSLog(@"str9的引用计数=%ld",[str9 retainCount]);
//---- 7 字符串的比较
NSString *str11 = [[NSString alloc] initWithFormat:@"%@%d",@"a",2];
NSString *str12 = [[NSStringalloc] initWithFormat:@"%@%d",@"b",2];
if ([str11 isEqualToString:str12]) {
NSLog(@"内容相等,%p,%p",str11,str12);
}else{
NSLog(@"内容不相等,%p,%p",str11,str12);//只是内容
}
if (str11 == str12) {
NSLog(@"是否为同一个对象"); //内存地址也要相同
}
//---- 8 字符串的比较(升序降序还有相等)
NSComparisonResult result = [str11 compare:str12];
if (result == NSOrderedAscending) {//升序
NSLog(@"str11<12");
}
else if(result == NSOrderedDescending)//降序
{
NSLog(@"str11 >12");
}
else if(result == NSOrderedSame)//相等
{
NSLog(@"相等");
}
//-9 --判断字符串的 开始 和结束 ///hasPrefix
NSString *str13 =@"ios-iphone-com";
if ([str13 hasPrefix:@"ios"]) {
NSLog(@"是的ios");
}
if ([str13 hasSuffix:@"com"]) {
NSLog(@"是的com");
}
[str13 release];
/*========10、将一个字符串转化为一个数组======================*/
//只有有特殊分割的才可以转化为一个数组
NSString *str18=@"qwe&asd&zxc";
NSArray *array= [str18componentsSeparatedByString:@"&"];
NSLog(@"array=%@",array);
/*========11、字符串的截取===================================*/
NSString *str19=@"qwertuyuiop";
NSString *substr1 = [str19substringFromIndex:2];//从第2个字母开始截(不包括第2个字母)
NSString *substr2=[str19 substringToIndex:2];//从头开始截取截到第2个字母(包括第二个字母)
NSRange rang= NSMakeRange(2, 5);//NSRang是一个范围函数(2,5),2是代表从2+1开始,5是截取5位
NSString *substr3=[str19 substringWithRange:rang];
NSLog(@"substr1=%@",substr1);
NSLog(@"substr2=%@",substr2);
NSLog(@"substr3=%@",substr3);
/*========12、求字符串的长度===================*/
NSString *str20=@"asdfghjkl";
NSInteger length=[str20 length];
NSLog(@"length=%ld",length);
/*========13、字符串的大小写====================*/
NSString *str21=@"heLLo woRld";
NSString *upstr=[str21uppercaseString];
NSLog(@"upstr=%@",upstr);
NSString *lowstr=[str21lowercaseString];
NSLog(@"lowstr=%@",lowstr);
NSString *capstr=[str21capitalizedString];
NSLog(@"capstr=%@",capstr);
/*========14、字符串的拼接======================*/
NSString *str22=@"abc";
NSString *str23=@"efg";
NSString *appstr1=[[NSStringalloc]initWithFormat:@"%@%@",str22,str23];
NSLog(@"appstr1=%@",appstr1);
//stringByAppendingString样子像类方法,但它是个实例方法
NSString *appstr2=[str22stringByAppendingString:str23];
NSLog(@"appstr2=%@",appstr2);
NSString *appstr3=[str22stringByAppendingFormat:@"%@",str23];
NSLog(@"appstr3=%@",appstr3);
[appstr1 release];
[appstr2 release];
[appstr3 release];
/*========15、字符串和基本数据类型的转换===================*/
NSString *str24=@"3.14";
float fstr=[str24floatValue];//解封
NSLog(@"fsrt=%.2f",fstr);//%.2f表示保留两位小数
/*========16、字符串的查找===============================*/
NSString *str25=@"qwertyuiop";
NSRange rang1=[str25 rangeOfString:@"ertyu"];
if (rang1.location!=NSNotFound)//NSNotFound是系统的最大数
{
NSLog(@"包含有想要的字符串");
NSLog(@"他在%ld位置",rang1.location);
NSLog(@"它的长度为%ld",rang1.length);
}
/*========17、可变字符串=================================*/
NSMutableString *mutablrstr1=[[NSMutableStringalloc]initWithFormat:@"abc"];
NSLog(@"mutablestr1=%@",mutablrstr1);
[mutablrstr1 insertString:@"qwe"atIndex:3];
NSLog(@"mutablestr1=%@",mutablrstr1);
[mutablrstr1 replaceCharactersInRange:NSMakeRange(3, 3)withString:@"xyz"];
NSLog(@"mutablestr1=%@",mutablrstr1);
[mutablrstr1 deleteCharactersInRange:NSMakeRange(3, 3)];
NSLog(@"mutablestr1=%@",mutablrstr1);