天天看點

【黑馬程式員】————Foundation架構02常用類—字元類

------Java教育訓練、Android教育訓練、iOS教育訓練、.Net教育訓練、期待與您交流! -------

 常用類

NSString

->NSMutableString

NSArray

->NSMutableArray

NSSet

->NSMutableSet

NSDictionary

->NSMutableDictionary

NSDate

NSObject

字元類-NSString/NSMutableString

 NSString : 不可變字元串

 NSMutableString : 可變字元串

1 void stringCreate()
 2 {
 3     /*
 4      1.字元串的建立
 5      */
 6     NSString *s1 = @"jack";
 7     
 8     //NSString *s2 = [[NSString alloc] initWithString:@"jack"];
 9     
10     NSString *s3 = [[NSString alloc] initWithFormat:@"age is %d", 10];
11     
12     // C字元串 --> OC字元串
13     NSString *s4 = [[NSString alloc] initWithUTF8String:"jack"];
14     // OC字元串 --> C字元串
15     const char *cs = [s4 UTF8String];
16     
17     // NSUTF8StringEncoding 用到中文就可以用這種編碼
18     NSString *s5 = [[NSString alloc] initWithContentsOfFile:@"/Users/apple/Desktop/1.txt" encoding:NSUTF8StringEncoding error:nil];
19     
20     
21     // URL : 資源路徑
22     // 協定頭://路徑
23     // file://
24     // ftp://
25     // http://weibo.com/a.png
26     
27     
28 
29     
30     // NSURL *url = [[NSURL alloc] initWithString:@"file:///Users/apple/Desktop/1.txt"];
31     NSURL *url = [NSURL fileURLWithPath:@"/Users/apple/Desktop/1.txt"];
32     
33     NSString *s6 = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
34     NSLog(@"s6=\n%@", s6);
35     
36     
37     /*
38      一般都會有一個類方法跟對象方法配對
39      [NSURL URLWithString:<#(NSString *)#>];
40      [NSString stringWithFormat:@""];
41      [NSString stringWithContentsOfFile:<#(NSString *)#> encoding:<#(NSStringEncoding)#> error:<#(NSError *__autoreleasing *)#>];
42      
43      */
44 
45 }      
1 void stringExport()
 2 {
 3     // 字元串的導出
 4     [@"Jack\nJack" writeToFile:@"/Users/apple/Desktop/my.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
 5     
 6     
 7     NSString *str = @"4234234";
 8     NSURL *url = [NSURL fileURLWithPath:@"/Users/apple/Desktop/my2.txt"];
 9     [str writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:nil];
10 }      
1 void stringExport()
 2 {
 3        NSMutableString *s1 = [NSMutableString stringWithFormat:@"my age is 10"];
 4     // 拼接内容到s1的後面
 5     [s1 appendString:@" 11 12"];
 6     
 7     // 擷取is的範圍
 8     NSRange range = [s1 rangeOfString:@"is"];
 9     // 删除
10     [s1 deleteCharactersInRange:range];
11     
12     
13     NSString *s2 = [NSString stringWithFormat:@"age is 10"];    
14     //新創了一個字元串
15     
16     NSString *s3 = [s2 stringByAppendingString:@" 11 12"];
17     
18     
19     NSLog(@"s1=%@, s2=%@", s1, s2);
20     
21     return 0;
22 }      

轉載于:https://www.cnblogs.com/Jr-711/p/3932813.html