2011-01-22 114 views
0

我想操纵的NSString等作为的NSString操纵

(0)喜欢的(1)。 (见(2))

(0)=拉曼

(1)=你

(2)= ThisGift

拉曼喜欢你。 (看这个礼物)

我不知道什么接近可以解决这个问题。

由于事先 问候

Venkat。

回答

0

-[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"]; 
2

如果你被允许更改模板格式,您可以使用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"];