2 bluetooth99 bluetooth99 于 2013.01.04 13:23 提问

动态添加字符串到NSString中

NSString的内容来自一个URL,我希望添加一些字符串到里面。而且这个NSString会实时改变。

比如:

获取的数据是:" hi mac this is ios"然后字符串就会改变为:" hi ios this mac" or hi this is mac ios"

mac后面就加上hello:变成这样:

" hi mac hello this is ios"
" hi ios this mac hello"
"hi this is mac hello ios"

该如何实现?

2个回答

MrsSeven7
MrsSeven7   2013.01.04 16:22
已采纳
string = [string stringByReplacingOccurrencesOfString:@"Mac" withString:@"Mac Hello"];
lanhailvcao
lanhailvcao   2013.01.04 15:55
NSMutableString *string=[NSMutableString stringWithString:@"hi mac this is ios"];
NSRange range = [string rangeOfString:@"mac"];    
[string insertString:@" hello" atIndex:range.location+3];
NSLog(@"str=%@",string);

最后输出:

str=hi mac hello this is ios
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!