Objective-C——Foundation框架——OC字符串(一)

OC字符串


———- android培训java培训、期待与您交流! ———-


网上看到的错误教程分析

    //创建空字符串,给予赋值 
    NSString *astring = [[NSString alloc] init];  
    astring = @"This is a String!";  
    NSLog(@"astring:%@",astring);  
    [astring release];  

    //在以上方法中,提升速度:initWithString方法   
    NSString *astring = [[NSString alloc] initWithString:@"This is a String!"];  
    NSLog(@"astring:%@",astring);  
    [astring release]; 

为什么说上面的教程是错的呢?好像也能正常运行没有报错啊.
是滴.上面代码的确不会报错也能正常运行。
但是, 要知道NSString类型是不可变的字符串类型当对象创建后就不能对其进行改变了,而NSString * astring 只是对我们创建的字符串进行了引用,astring并不是我们创建的NSString对象只是对他的指引或者说是引用,而当执行astring = @”This is a String!”; 这条语句的时候相当于在内存中新开辟了一块内存空间用来存储OC字符串对象,而这时astring只是从指向原来的NSString字符串对象转变成指向新创建的字符串对象,而不是改变了原来的字符串给空字符串赋值!

下面我们来证明一下:

        NSString *oc_str_tmp = [[NSString alloc] init];
        NSLog(@"--- %p ---", oc_str_tmp);
        oc_str_tmp = @"Better early than late!";
        NSLog(@"--- %p ---", oc_str_tmp);

        [oc_str_tmp release];
        oc_str_tmp = nil;

        NSString *oc_str_tmp2 = [[NSString alloc] initWithString:@"Better early than late!"];
        NSLog(@"%@......%p", oc_str_tmp2, oc_str_tmp2);

        [oc_str_tmp2 release];
        oc_str_tmp2 = nil;

Output:
输出

从输出我们可以看到在进行字符串引用切换后,NSString类型指针oc_str_tmp的指向已经发生改变
而从第三句输出结果的地址信息来看,我们又能得到另外两个结论:

  • 1.以@”… …”;方式创建不可变字符串对象是[[NSString alloc] initWithString:@”… …”];的简单写法
  • 2.对已经创建了的常量字符串,将不会再次分配内存空间存储该字符串,而是将已有的该常量字符串的地址返回给NSString类型的指针

可变字符串NSMutableString

        //可变字符串
        NSMutableString *oc_mutable_str = [[NSMutableString alloc] init];
        NSLog(@"--- %p ---", oc_mutable_str);
        [oc_mutable_str appendFormat:@"Better early than late!"];
        NSLog(@"--- %p ---", oc_mutable_str);

        [oc_mutable_str release];
        oc_mutable_str = nil;

Output:
这里写图片描述

这样用可变字符串即可实现创建空字符串然后对其赋值的操作


字符串的一般使用

1.字符串拼接

        NSString *oc_str = @"张飞";
        NSLog(@"%@", oc_str);
        //返回字数长度
        NSLog(@"length of oc_str is %d", oc_str.length);

        oc_str = nil;
        //字符串拼接
        NSLog(@"---------- 字符串拼接1 ----------");
        oc_str = @"Better early than";
        NSLog(@"%@", oc_str);
        NSString *new_oc_str = [oc_str stringByAppendingFormat:@" late!"];
        NSLog(@"%@", oc_str);
        NSLog(@"%@", new_oc_str);

        new_oc_str = nil;
        NSLog(@"---------- 字符串拼接2 ----------");
        new_oc_str = [NSString stringWithFormat:@"%@ late!", oc_str];
        NSLog(@"%@", new_oc_str);
        oc_str = nil;
        new_oc_str = nil;

2.NSNumber转为NSString类型

PS:(放这有点小不合适感脚-.- ,不过既然练了就拿出来吧…..)

 //NSNumber转为NSString类型
        NSLog(@"---------- NSNumber转NSString1 ----------");
        NSNumber *oc_num1 = [[NSNumber alloc] initWithInt:10];
        oc_str = [oc_num1 stringValue];
        NSLog(@"%@", oc_str);

        [oc_num1 release];
        oc_num1 = nil;
        oc_str = nil;
        NSLog(@"---------- NSNumber转NSString2 ----------");
        oc_num1 = [NSNumber numberWithInt:33];
        oc_str = [oc_num1 stringValue];
        NSLog(@"%@", oc_str);

        oc_num1 = nil;
        oc_str = nil;
        NSLog(@"---------- NSNumber转NSString3 ----------");
        oc_num1 = [NSNumber numberWithInt:45];
        NSNumberFormatter * oc_format_num1= [[NSNumberFormatter alloc] init];
        oc_str = [oc_format_num1 stringFromNumber: oc_num1];
        NSLog(@"%@", oc_str);

        [oc_format_num1 release];
        oc_format_num1 = nil;
        oc_num1 = nil;
        oc_str = nil;

3.字符查找范围

oc_mutable_str = [NSMutableString initWithString:@"Better early than late!"];
NSRange * oc_range = [oc_mutable_str rangeOfString:@"than"];
//删除可变字符串中,所查找到的字符
[oc_mutable_str deleteCharactersInRange:oc_range]; 

NSLog(@"%@", oc_mutable_str);

4.拆分字符串

oc_str = [[NSString alloc] initWithString:@"Better early than late!"];
NSArray *oc_array = [oc_str componentsSeparatedByString:@" "];
for(NSString *tmp in oc_array)
{
    NSLog(@"%@", tmp);
}

字符串文件操作

//从文件中读取数据
oc_str = [[NSString alloc] initWithContentsOfFile:@"/Users/chenlong/Desktop/111" encoding:NSUTF8StringEncoding error:nil];

NSLog(@"%@", oc_str);

//将字符串写入文件
[oc_str writeToFile:@"/Users/chenlong/Desktop/tmp" atomically:YES encoding:NSUTF8StringEncoding error:nil]

题目

求字符串“100”和“150”按十进制数值做差后的结果以字符串形式输出

    //求字符串“100”和“150”按十进制数值做差后的结果以字符串形式输出

        NSString *oc_str_num1 = @"100";
        NSString *oc_str_num2 = @"150"; 

        int num1 = [oc_str_num1 intValue];
        int num2 = [oc_str_num2 intValue]; 

        NSLog(@"100-150 = %d", num1-num2);

        oc_str_num1 = nil;
        oc_str_num2 = nil;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值