我想像这样操作NSString
(0)喜欢(1)。(请参见。(2)
(0) =拉曼
(1) =你
(2) = ThisGift
至
拉曼喜欢你。(请参见。ThisGift)
我不知道什么方法可以解决这个问题。
提前感谢,致以敬意
文卡特。
发布于 2011-01-22 15:38:24
-[NSString stringByReplacingOccurrencesOfString:withString:]。
您可以这样使用它:
NSString * source = @"(0) Likes (1). (see. (2))";
source = [source stringByReplacingOccurrencesOfString:@"(0)" withString:@"Raman"];
NSLog(@"%@", source); //logs "Raman Likes (1). (see. (2))"发布于 2011-01-22 16:04:48
如果允许您更改模板格式,则可以使用format strings。
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"];或者(如果格式字符串可以始终依赖于参数的替换顺序):
NSString *template = @"%@ Likes %@. (see. %@)";
NSString *msg = [NSString stringWithFormat:template,@"Raman",@"You",@"ThisGift"];发布于 2011-01-22 15:39:52
请参阅-[NSString stringByReplacingOccurrencesOfString:withString:]
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"];https://stackoverflow.com/questions/4766707
复制相似问题