OC基礎--經常使用類的初步介紹與簡單實用之NSString

1、NSString:不可變字符串/NSMutableString:可變字符串app

1>字符串的經常使用建立方式:atom

  (1)NSString *s1 = @"Chaos";url

  (2)NSString *s2 = [[NSString alloc] initWithFormat:@"age is %d",20];orm

  (3)C字符串--》OC字符串資源

  NSString *s3 = [[NSString alloc] initWithUTF8String:"Chaos"];字符串

  (4)OC 字符串--》C字符串get

  const char *cs = [s3 UTF8String];it

2>字符串的導入與導出table

經過URL(資源路徑)來操做:URL格式常見--協議頭://路徑ftp

常見的協議頭---file://  ftp://  http://

(1)經過URL讀取字符串--導入

    // 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);

(2)字符串的導出

    // 字符串的導出

    [@"Jack\nJack" writeToFile:@"/Users/apple/Desktop/my.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];

    NSString *str = @"4234234";

    NSURL *url = [NSURL fileURLWithPath:@"/Users/apple/Desktop/my2.txt"];

    [str writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:nil];

相關文章
相關標籤/搜索