2009-12-02 46 views
2

我有這個快速的問題,我需要解決。我這裏有一個字符串,像這樣:字符串和變量幫助需要 - iphone sdk

textbox.text [email protected]"Your name is" 

然後我想補充權後,「你的名字是」顯示文本的變量。

所以在Visual Basic中我瞭解到像這樣......

textbox.text [email protected]"Your name is" & variable1. 

,但現在我可以看到,它不可可這樣的工作。

請幫我解決這個...

回答

5
textbox.text = [NSString stringWithFormat:@"Your name is %@", variable1]; 

閱讀文檔stringWithFormat:瞭解字符串格式說明。基本上,你有一個格式字符串,其中包含代碼%@,以下參數代替這些轉義代碼。

它具有與舊C型printf()函數相同的語法。可可的記錄功能,NSLog(),也是一樣的。

如果您需要將很多字符串組合在一起,請嘗試閱讀NSMutableString

你也可以這樣做:

textbox.text = [@"Your name is " stringByAppendingString:variable1]; 

但如果你有來連接兩個以上的東西,stringWithFormat:更加簡練。

0

使用NSString的stringByAppendingString:方法:

textbox.text = [@"Your name is" stringByAppendingString:variable1];