Foundation框架中的字符串(NSString/NSMutableString)

一、NSString

    1. 字符串的创建

NSString *s1 = @"jack";
NSString *s2 = [[NSString alloc] initWithString:@"jack"];
NSString *s3 = [[NSString alloc] initWithFormat:@"age is %d", 10];

// C字符串转成OC字符串
NSString *s4 = [[NSString alloc] initWithUTF8String:"jack"];

// OC字符串转成C字符串
const char *cs = [s4 UTF8String];


    2. 读取文件中的内容

// 使用文件路径
NSString *s5 = [[NSString alloc] initWithContentsOfFile:@"/Users/apple/Desktop/1.txt"
 encoding:NSUTF8StringEncoding error:nil];

// 使用资源路径
NSURL *url = [[NSURL alloc] initWithString:@"file:///Users/apple/Desktop/1.txt"];
NSString *s6 = [[NSString alloc] initWithContentsOfURL:url
 encoding:NSUTF8StringEncoding error:nil];

资源路径(URL)由两部分组成:协议头+路径,协议头分三种:1. http:// 2. file:// 3. ftp://。

二、NSMutableString

    1. 可变字符串的创建

NSMutableString *s1 = [NSMutableString stringWithFormat:@"age is 10"];

    2. 在字符串s1后面再拼接另一个字符串

[s1 appendString:@" 11 12"];

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值