首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSString操作

NSString操作
EN

Stack Overflow用户
提问于 2011-01-22 15:32:28
回答 3查看 735关注 0票数 0

我想像这样操作NSString

(0)喜欢(1)。(请参见。(2)

(0) =拉曼

(1) =你

(2) = ThisGift

拉曼喜欢你。(请参见。ThisGift)

我不知道什么方法可以解决这个问题。

提前感谢,致以敬意

文卡特。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-01-22 15:38:24

-[NSString stringByReplacingOccurrencesOfString:withString:]

您可以这样使用它:

代码语言:javascript
复制
NSString * source = @"(0) Likes (1). (see. (2))";
source = [source stringByReplacingOccurrencesOfString:@"(0)" withString:@"Raman"];
NSLog(@"%@", source);  //logs "Raman Likes (1). (see. (2))"
票数 5
EN

Stack Overflow用户

发布于 2011-01-22 16:04:48

如果允许您更改模板格式,则可以使用format strings

代码语言:javascript
复制
NSString *template1 = @"%1$@ Likes %2$@. (see. %3$@)",
         *template2 = @"%2$@ got a %3$@ from %1$@.";
NSString *msg1 = [NSString stringWithFormat:template1,@"Raman",@"You",@"ThisGift"],
         *msg2 = [NSString stringWithFormat:template2,@"Raman",@"You",@"ThisGift"];

或者(如果格式字符串可以始终依赖于参数的替换顺序):

代码语言:javascript
复制
NSString *template = @"%@ Likes %@. (see. %@)";
NSString *msg = [NSString stringWithFormat:template,@"Raman",@"You",@"ThisGift"];
票数 2
EN

Stack Overflow用户

发布于 2011-01-22 15:39:52

请参阅-[NSString stringByReplacingOccurrencesOfString:withString:]

代码语言:javascript
复制
NSString *foo = @"(0) likes (1). (see (2))";
NSString *bar;
bar = [foo stringByReplacingOccurrencesOfString:@"(0)"
                                     withString:@"Raman"];
bar = [bar stringByReplacingOccurrencesOfString:@"(1)"
                                     withString:@"You"];
bar = [bar stringByReplacingOccurrencesOfString:@"(2)"
                                     withString:@"ThisGift"];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4766707

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档