首页 > 代码库 > 【黑马程序员】————Foundation框架02常用类—字符类

【黑马程序员】————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.png26     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 }

 

【黑马程序员】————Foundation框架02常用类—字符类