-----------android培训、java培训、java学习型技术博客、期待与您交流!------------
-----------并不是生来就是大神、代码量决定一切、其他都是假象、大家都不是傻子对吧?-----------
-----------------------------------------------------------------------------
字符串的创建
NSString *s1 = @"123123";
NSString *s2 = [[NSString alloc] initWithString:@"jack"];
NSString *s3 = [[NSString alloc] initWithFormat:@"age is %d",10];
//c字符串转换成oc字符串
NSString *s4 = [[NSString alloc] initWithUTF8String:"jack"];
//oc字符串转换成c字符串
char *cs = [s4 UTF8String];
char *c = "yes i love you";
NSString *s = [NSString stringWithUTF8String:c];
//NSUTF85StringEncoding 用到中文
//问:什么要学URL?
//答:因为我们需要读取和连接很多资源信息 通过URL就可以做到
// URL基本格式
//URL:资源路径协议头: //包含格式:file://本地路径 ftp:// ftp服务器路径 http://
//如file:///Users/lulu/Desktop/1.txt/http://www.baidu.com/
//要注意 file:// 是个整体 所以后面是3个斜杠 file://+/Users/….
//利用对象方法来创建一个url地址
NSURL *url = [[NSURL alloc] initWithString:@"file:///Users/lulu/Desktop/1.txt"];
//利用对象方法读取url地址
NSString *s = [[NSString alloc ] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",s );
//读取本地文件的另一种方法file协议
//意思是创建一个URL他的地址来自于file 协议的/users/...../1.txt
NSURL *url = [NSURL fileURLWithPath:@"/Users/lulu/Desktop/1.txt"];
//将从url获取的文件内容转换成NSString 类型然后打印出来显示全部内容
NSString *s = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",s);
//抓百度 创建一个http网络协议的url 一般不用这种方法 因为有alloc需要内存管理 一般用类方法创建下一块
NSURL *Baiduurl = [[NSURL alloc] initWithString:@"http://www.baidu.com/"];
//将获取的资源转换成NSString打印出来
NSString *s6 = [[NSString alloc] initWithContentsOfURL:Baiduurl encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",s6);
-------------------------------------------------
编程时候最常用的方法利用类方法了来创建url对象
//用类方法了来创建一个url地址
NSURL *url= [NSURL URLWithString:@"file:///Users/lulu/Desktop/1.txt"];
//利用类方法将url的资源转换成NSString类型然后得到内容打印
NSString *s = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",s);
//抓百度
NSURL *baiduURL = [NSURL URLWithString:@"http://www.baidu.com/"];
NSString *s2 = [NSString stringWithContentsOfURL:baiduURL encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",s2);
--------------------------------------------------
匿名url方法
// 利用NSString 类方法 调用stringWithContentsOfURL:获取资源返回NSString类型 一气呵成 以后最好都这样写 多敲就记得了
NSString *s = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.baidu.com/"] encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",s);
-----------------------------------------------------
//自动创建文件方法如果2.txt文件存在它会写入如果不存在他会创建
//关于atomically的作用老师说是原子型操作,这里写入yes
//atomically作用:如果你在写入文件的时候失败了它不会在路径里创建txt文件如果写no就算写入失败也会创建txt文件
[@"yes i love you" writeToFile:@"/Users/lulu/Desktop/2.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSString *str = @“yes i love you LP”; //同上
[str writeToFile:@"/Users/lulu/Desktop/2.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
//将百度的页面抓取到桌面创建一个txt文件
//定义一个百度的url地址
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com/"];
//抓取百度页面的内容
NSString *st = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
//将被容以txt的方式保存到桌面路径中
[st writeToFile:@"/Users/lulu/Desktop/4.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
//结果在桌面创建了一个4.txt文件内容则是百度页面的内容
//快速获取一个文档 多敲多练 可读性还算不错 反正我是能看懂
// 利用NSString调用类方法stringWithContetsOfURL 获取 利用NSURL 调用类方法URLWithString 获取http://www.baidu.com资源 返回的是NSString类型 然后打印
NSLog(@"%@", [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.baidu.com"] encoding:NSUTF8StringEncoding error:nil]);
-------------------------------------------------------------------------------------------
NSMutableString 可变字符串
//创建一个可变字符串的方法
//对象方法创建一般不适用因为需要retain
NSMutableString *st = [[NSMutableString alloc ] initWithFormat:@"age is 10"];
//类方法创建常用
NSMutableString *str = [NSMutableString stringWithFormat:@"age is 10"];
//给字符串增加字符的方法
[str appendString:@" 11 12"];
NSLog(@"%@",str);
NSMutableString *str = [NSMutableString stringWithString:@"yes i you "];
//从指定位置插入字符串
[str insertString:@" love" atIndex:5];
NSString *str2 = [NSString stringWithFormat:@"yes i love you can you know"];
//获取指定位置以后的所有字符串
NSString *str3 = [str2 substringFromIndex:5];
//获取从0位置开始到指定位置的字符串
NSString *str4 = [str2 substringToIndex:14];
[str deleteCharactersInRange:[str rangeOfString:@"you"]];
--------------------------------------------------------------------------------------------
写着玩的联系一下思维
//抓取百度页面的内容在桌面创建一个名为4.txt的文档将页面被容保存到文档中
[[NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.baidu.com"] encoding:NSUTF8StringEncoding error:nil] writeToFile:@"/Users/lulu/Desktop/4.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];