2011-01-22 121 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"];