标签:
1 #import <Foundation/Foundation.h> 2 3 /** 4 NSString:不可变字符串 5 NSMutableString:可变字符串 6 */ 7 8 int main(int argc, const char * argv[]) { 9 @autoreleasepool { 10 /** 1.字符串的创建 */ 11 NSString *s1 = @"Chris Paul"; 12 NSString *s2 = [[NSString alloc] initWithFormat:@"Magic Johnson"]; 13 14 /** C字符串 --> OC字符串 */ 15 NSString *s3 = [[NSString alloc] initWithUTF8String:"Kobe Bryant"]; 16 17 /** OC字符串 --> C字符串 */ 18 const char * cs = [s3 UTF8String]; /** const常量 */ 19 20 /** NSUTF8StringEncoding 用到中文就可以用这种编码,记住即可 */ 21 NSString *s4 = [[NSString alloc] initWithContentsOfFile:@"/Users/tusdk/Desktop" encoding:NSUTF8StringEncoding error:nil]; 22 23 /** URL:资源路径 24 协议头://路径 25 file:// 本地文件 26 ftp:// 服务器文件 27 */ 28 NSURL *url = [[NSURL alloc] initWithString:@"http://www.wuhunews.cn"]; 29 NSString *s5 = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]; 30 NSLog(@"%@", s5); 31 32 /** 33 一般都有个类方法与对象方法配对 34 [NSURL URLWithString:(NSString *)]; 35 [NSString stringWithFormat:(NSString *), ...]; 36 [NSString stringWithContentsOfFile:(NSString *) encoding:(NSStringEncoding) error:(NSError *__autoreleasing *)]; 37 38 [NSURL fileURLWithPath:(NSString *)]; // 有点特殊 39 */ 40 41 42 /** 字符串写入文件 */ 43 [@"Michael Jordan" writeToFile:@"/Users/tusdk/Desktop/Michael Jordan.m" atomically:YES encoding:NSUTF8StringEncoding error:nil]; 44 /** atomically:YES 原子性:如果中途写入失败,test.docx不会被创建 */ 45 46 /** 可变字符串的创建与拼接 */ 47 NSMutableString *s6 = [NSMutableString stringWithFormat:@"Chris Paul"]; 48 [s6 appendFormat:@"Magic Johnson"]; /** 拼接内容到s6的后面 */ 49 50 /** 删除字符串方法1: */ 51 [s6 deleteCharactersInRange:NSMakeRange(4, 2)]; /** 4是起点,2是长度*/ 52 /** 删除字符串方法2: */ 53 NSRange range = [s6 rangeOfString:@"Paul"]; 54 [s6 deleteCharactersInRange:range]; 55 56 NSLog(@"%@", s6); 57 58 NSString *s7 = [s6 stringByAppendingString:@"Michael Jordan, Kobe Bryant"]; 59 } 60 return 0; 61 }
标签:
原文地址:http://www.cnblogs.com/herui1991/p/4733360.html