foundation的使用——NSString

/*

 NSString : 不可变字符串

 

 NSMutableString : 可变字符串

 */

    

    /*

     1.字符串的创建

     */

    NSString *s1 = @"jack";

    

    //NSString *s2 = [[NSString alloc] initWithString:@"jack"];

    

    NSString *s3 = [[NSString allocinitWithFormat:@"age is %d"10];

    

    // C字符串 --> OC字符串

    NSString *s4 = [[NSString allocinitWithUTF8String:"jack"];

    // OC字符串 --> C字符串

    const char *cs = [s4 UTF8String];

    

    // NSUTF8StringEncoding 用到中文就可以用这种编码

    NSString *s5 = [[NSString allocinitWithContentsOfFile:@"/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 allocinitWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];

    NSLog(@"s6=\n%@", s6);

 /*

     一般都会有一个类方法跟对象方法配对

     [NSURL URLWithString:<#(NSString *)#>];

     [NSString stringWithFormat:@""];

     [NSString stringWithContentsOfFile:<#(NSString *)#> encoding:<#(NSStringEncoding)#> error:<#(NSError *__autoreleasing *)#>];

     

     */

   //可变长字符串的创建 

    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"];

     

    NSLog(@"s1=%@, s2=%@", s1, s2);

    

    // 字符串的导出

    [@"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];

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值