黑马程序员技术交流社区
标题:
关于OC字符串第三部分
[打印本页]
作者:
wanyiyuan
时间:
2014-9-24 15:13
标题:
关于OC字符串第三部分
>>>>>>基本数据类型(int float,double char)<<<<<<<<
//14.int类型换换成字符串
int a = 10;
NSString * str1 = [NSString stringWithFormat:@"%d",a];
NSLog(@"str1 %@",str1);
//15.float -> NSString
float f = 2.4;
NSString * str3 = [NSString stringWithFormat:@"%.1f",f];
NSLog(@"str3 %@",str3);
//16.double - >NSstring
double d = 65.9898;
NSString * str4 = [NSString stringWithFormat:@"%.4f",d];
NSLog(@"str4 %@",str4);
//17.char - > NSString
char c = 'c';
NSString * str5 = [NSString stringWithFormat:@"%c",c];
NSLog(@"str5 %@",str5);
//18.NSString -> int
NSString * str6 = @"1";
int i1 = [str6 intValue];
NSLog(@"i1 %d",i1);
//19.NSString -> float
NSString * str7 = @"9.8";
float f1 = [str7 floatValue];
NSLog(@"f1 %.1f",f1);
//20.NSString -> double
NSString * str8 = @"8.88";
double d1 = [str8 doubleValue];
NSLog(@"d1 %.2f",d1);
//注意:当你要把字符串转换成某种基本数据类型的时候,一定要确定你要转化你的数据是正确
NSString * str9 = @"i386";
int i2 = [str9 intValue];
NSLog(@"is %d",i2);
//21.将字符串中的字母转换成大写
NSString * str10 = @"china";
NSString * tmpStr1 = [str10 uppercaseString];
NSLog(@"tmpStr1 %@ str10 %@",tmpStr1,str10);
//22.将字符中的字母转换成小写
NSString * str11 = @"CHINA";
NSString * tmpStr2 = [str11 lowercaseString];
NSLog(@"tmpStr2 %@",tmpStr2);
//23.将首字母大写
NSString * str12 = @"china";
NSString * tmpStr3 = [str12 capitalizedString];
NSLog(@"tmpStr3 %@",tmpStr3);
>>>>字符串重组<<<<<
//1.多个数据拼接成一个字符串
int year = 1988;
int month = 4;
int day = 15;
NSString * date = [NSString stringWithFormat:@"%d年%d月%d日",year,month,day];
NSLog(@"date %@",date);
NSString * address = @"昌平区,金燕龙";
NSString * dateAndAddrss = [NSString stringWithFormat:@"%@地址:%@",date,address];
NSLog(@"dateAndAddress %@",dateAndAddrss);
//2.字符串的末尾追加新的字符
NSString * str1 = @"www.";
NSString * str2 = [str1 stringByAppendingString:@"itcast.cn"];
NSLog(@"str2 %@ str1 %@",str2,str1);
//3.在制定的范围追加字符串
NSString * str3 = @"www.cn";
NSRange range = {4,0};
// 4 location代表的时从哪个索引开始插入,length 代表的意思可以覆盖几个字符
NSString * str4 = [str3 stringByReplacingCharactersInRange:range withString:@"itcast."];
NSLog(@"str4 %@",str4);
//4.使用新的字符,替换原有的字符
NSString * str5 = @"www.itcast.cn";
NSString * str6 = [str5 stringByReplacingOccurrencesOfString:@"w" withString:@"a"];
NSLog(@"str6 %@",str6);
//5.在字符串中删除特定的某些字符 胡汉三 hhs
NSString * str7 = @"www---.itcatst.--cn";
NSString * str8 = [str7 stringByReplacingOccurrencesOfString:@"-" withString:@""];
NSLog(@"str8 %@",str8);
//6.去掉字符串中的空格
NSString * str9 = @"www .itcat .cn";
NSString * str10 = [str9 stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"str10 %@",str10);
>>>>>字符串写入文件<<<<<<
//将字符串写入到文件
NSString * content = @"床前明月光";
NSString * path = @"/Users/aplle/Desktop/共享课堂/mytest.txt";
//1.参数1 要给入文件目录(并且要包括文件名称)
//2.atomically : YES
//3.NSUTF8StringEncoding 国际通用的编码格式
//4.NSError 是用来记录错误信息的
NSError * error;
BOOL isWriteSuccess = [content writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
if(isWriteSuccess)
{
NSLog(@"文件创建成功");
}
else
{
NSLog(@"error %@",error);
}
>>>>>从文件中读取字符串<<<<<
/要给出文件路径
//你写入文件的时候,使用的是什么编码格式,读取的时候就要给我用什么编码格式
NSString * path = @"/Users/aplle/Desktop/共享课堂/mytest.txt";
NSError * error;
NSString * str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
if (error != nil)
{
NSLog(@"error message %@",error);
}
else
{
NSLog(@"str content %@",str);
}
>>>>>>可变长度字符串<<<<<<
//1.创建可变字符串
//NSMutableString 继承自 NSString
//NSMutableString 拥有NSString所有的属性以及方法
// NSMutableString * muStr = [[NSMutableString alloc] init];
NSMutableString * muStr = [[NSMutableString alloc] initWithCapacity:0];
//是初始化字符串内容
[muStr setString:@"www"];
NSLog(@"mustr %p",muStr);
//动态的给字符串末尾追加新值
[muStr appendString:@".cn"];
//在指定的索引位置,插入字符串
[muStr insertString:@".itcast" atIndex:3];
//删除指定范围的字符串
//你要删除的长度,一定不能够超出字符串的的长度,导致异常Range or index out of bounds
NSRange range = {3,7};
// [muStr deleteCharactersInRange:range];
[muStr replaceCharactersInRange:range withString:@"传智播客"];
NSLog(@"muStr %@",muStr);
>>>>>常用结构体<<<<<<
//1.创建一个NSRange结构体 表示范围
NSRange range = {4,5};
NSRange range2 = NSMakeRange(4, 5);
NSString * strRange = NSStringFromRange(range);
NSLog(@"strRange %@",strRange);
//2.NSSize 等同于 CGSize 有两个值组成 weight表示宽度 height高度
NSSize size = {21,19};
NSSize size2 = NSMakeSize(21, 19);
NSString *strSize = NSStringFromSize(size2);
NSLog(@"strSize %@",strSize);
//3.NSPoint 等同CGPoint 表示一个点的坐标位置
NSPoint point = {20,40};
NSPoint point2 = NSMakePoint(20, 40);
NSString * strPoint = NSStringFromPoint(point);
NSLog(@"strPoint %@",strPoint);
//4.NSRect
NSRect rect = {40,60,100,200};
NSRect rect2 = NSMakeRect(0, 10, 100, 100);
NSString * strRect = NSStringFromRect(rect);
NSLog(@"strRect %@",strRect);
>>>>基本数据类型包装类<<<<<<
//集合中,不能存储基本数据类型,如果你想要把基本数据类型存储到数组,集合中,就必须把它转换成基本数据类型的包装类
@autoreleasepool {
//int float double char
int a = 10;
// [NSString stringWithFormat:@"%d",a];
//1.使用NSNumber 包装 int数据类型
NSNumber * number1 = [NSNumber numberWithInt:a];
NSLog(@"%@",number1);
//2.直接转换成字符串
NSString * strInt = [number1 stringValue];
NSLog(@"strInt %@",strInt);
//3.float包装成NSNumber
NSNumber * number2 = [NSNumber numberWithFloat:98.98];
NSLog(@"number2 %@",[number2 stringValue]);
//4.包装类转换成基本数据类型
int b = [number1 intValue];
NSLog(@"%d",b);
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2