NSString中的stringByReplacingOccurrencesOfString

本文介绍了在iOS开发中使用NSString的stringByReplacingOccurrencesOfString函数进行文本替换的过程。通过一个示例说明了该函数如何读取textfield内容并替换textview中的指定文本。函数返回一个新的字符串对象,原始对象保持不变,并且新对象具有autorelease属性,无需手动释放。
摘要由CSDN通过智能技术生成

今天学习iOS开发中,关于textfield和textview的相关内容,其实也就是按照SAM ios6 application development 第7章中介绍的例子在编写了。其中程序的作用是可以读取textfield中的内容然后替换掉textview中标记出来的地方。

程序最后3行:

       self.theStory.text = [self.theTemplate.text
                          stringByReplacingOccurrencesOfString:@"<place>" withString:self.thePlace.text];
    NSLog(@"theStory:%@",self.theStory.text);
    self.theStory.text = [self.theStory.text
                          stringByReplacingOccurrencesOfString:@"<verb>" withString:self.theVerb.text];

    self.theStory.text = [self.theStory.text
                          stringByReplacingOccurrencesOfString:@"<number>" withString:self.theNumber.text];

刚开始的时候,我没有注意,将后两个语句中theStory写成了theTemplate,结果最后运行得到的结果都只是把<number>替换掉了,前两处标记都没有替换,当时没有仔细看,觉得很费解,后来就用NSLog 查看t
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值