2011-01-22 10 views
0

NSStringを次のように操作したいと思います。NSString操作

(0) (参照。(2))

(0)=ラマン

(1)=あなた

(2)= ThisGift

ラマンあなたが好きです。 (ThisGiftを参照してください)

私はこの問題を解決できるか知りません。事前に

おかげで、 よろしく

ヴェンカト。

答えて

5

-[NSString stringByReplacingOccurrencesOfString:withString:]

あなたはこのようにそれを使用する:あなたはテンプレート形式を変更することが許可されている場合は

NSString * source = @"(0) Likes (1). (see. (2))"; 
source = [source stringByReplacingOccurrencesOfString:@"(0)" withString:@"Raman"]; 
NSLog(@"%@", source); //logs "Raman Likes (1). (see. (2))" 
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"];