我想操纵的NSString等作为的NSString操纵
(0)喜欢的(1)。 (见(2))
(0)=拉曼
(1)=你
(2)= ThisGift
到
拉曼喜欢你。 (看这个礼物)
我不知道什么接近可以解决这个问题。
由于事先 问候
Venkat。
我想操纵的NSString等作为的NSString操纵
(0)喜欢的(1)。 (见(2))
(0)=拉曼
(1)=你
(2)= ThisGift
到
拉曼喜欢你。 (看这个礼物)
我不知道什么接近可以解决这个问题。
由于事先 问候
Venkat。
-[NSString stringByReplacingOccurrencesOfString:withString:]
。
你使用这样的:
NSString * source = @"(0) Likes (1). (see. (2))";
source = [source stringByReplacingOccurrencesOfString:@"(0)" withString:@"Raman"];
NSLog(@"%@", source); //logs "Raman Likes (1). (see. (2))"
见-[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"];
如果你被允许更改模板格式,您可以使用format strings。
NSString *template1 = @"%[email protected] Likes %[email protected] (see. %[email protected])",
*template2 = @"%[email protected] got a %[email protected] from %[email protected]";
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"];