----------------------
ASP.Net+Unity开发
、
.Net培训
、期待与您交流! ----------------------
2.导出字符串
1.创建NSString:
- NSString *s1 = @"jack";
- //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字符串
- const charchar *cs = [s4 UTF8String];
- // NSUTF8StringEncoding 用到中文就可以用这种编码
- NSString *s5 = [[NSString alloc] initWithContentsOfFile:@"/Users/apple/Desktop/1.txt" encoding:NSUTF8StringEncoding error:nil];
- // URL : 资源路径
- // 协议头://路径
- // file://
- // ftp://
- // http://weibo.com/a.png
- // http://www.baidu.com
- // NSURL *url = [[NSURL alloc] initWithString:@"file:///Users/apple/Desktop/1.txt"];
- NSURL *url = [NSURL fileURLWithPath:@"/Users/apple/Desktop/1.txt"];
- NSString *s6 = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
- NSLog(@"s6=\n%@", s6);
- /*
- 一般都会有一个类方法跟对象方法配对
- [NSURL URLWithString:<#(NSString *)#>];
- [NSString stringWithFormat:@""];
- [NSString stringWithContentsOfFile:<#(NSString *)#> encoding:<#(NSStringEncoding)#> error:<#(NSError *__autoreleasing *)#>];
- */
2.导出字符串
- //通过文件路径将字符串写入到my.txt文件中 如果没有该文件会自动创建一个, atomiclly填YES可以保证在写入失败的情况下不会创建my.txt文件
- [@"Jack\nJack" writeToFile:@"/Users/apple/Desktop/my.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
- //通过url写入
- NSString *str = @"4234234";
- NSURL *url = [NSURL fileURLWithPath:@"/Users/apple/Desktop/my2.txt"];
- [str writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:nil];
3.可变字符串 NSMutableString
- NSMutableString *s1 = [NSMutableString stringWithFormat:@"my age is 10"];
- // 拼接内容到s1的后面
- [s1 appendString:@" 11 12"];
- // 获取is的范围
- NSRange range = [s1 rangeOfString:@"is"];
- [s1 deleteCharactersInRange:range];
- NSString *s2 = [NSString stringWithFormat:@"age is 10"];
- NSString *s3 = [s2 stringByAppendingString:@" 11 12"]; //s2是不变的 返回一个新的字符串给s3
---------------------- ASP.Net+Unity开发 、 .Net培训 、期待与您交流! ----------------------