zoukankan      html  css  js  c++  java
  • OC基础--常用类的初步介绍与简单实用之NSString

    一、NSString:不可变字符串/NSMutableString:可变字符串

    1>字符串的常用创建方式:

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

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

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

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

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

      const char *cs = [s3 UTF8String];

    2>字符串的导入与导出

    通过URL(资源路径)来操作:URL格式常见--协议头://路径

    常见的协议头---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= %@", s6);

    (2)字符串的导出

        // 字符串的导出

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

  • 相关阅读:
    [Leetcode] 225. Implement Stack using Queues
    前端面试题2
    数据结构_stack
    数据结构 station
    数据结构_wow(泡泡的饭碗)
    数据结构_XingYunX(幸运儿)
    数据结构 nxd(顺序对)
    数据结构 hbb(汉堡包)
    数据结构 elegant_sequence(优雅的序列)
    数据结构 i_love(我喜欢)
  • 原文地址:https://www.cnblogs.com/gchlcc/p/5224803.html
Copyright © 2011-2022 走看看