如何使用佔位符,如C#Windows窗體應用程序的字符串連接...砂礦持有人
我已經試過控制檯應用程序,它工作正常,但在Windows窗體應用程序也無法正常工作
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello {0}", textBox1.Text);
}
輸出應該像 「你好+ TextBox.Text」,但它給人的 「Hello {0}」
如何使用佔位符,如C#Windows窗體應用程序的字符串連接...砂礦持有人
我已經試過控制檯應用程序,它工作正常,但在Windows窗體應用程序也無法正常工作
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello {0}", textBox1.Text);
}
輸出應該像 「你好+ TextBox.Text」,但它給人的 「Hello {0}」
使用String.Format()
MessageBox.Show(string.Format("Hello {0}", textBox1.Text));
MessageBox
的第二個參數適用於MessageBox
的標題。因此,此代碼MessageBox.Show("Hello {0}", textBox1.Text);
顯示MessageBox
,其中Hello {0}
爲文本,textBox1.Text
爲MessageBox
的標題。您可以使用String.Format
這樣的:
string result = string.Format("Hello {0}", textBox1.Text);
MessageBox.Show(result);
C#6.0支持串插
MessageBox.Show($"Hello {textbox1.Text}")
或者你可以像使用其他的String.format都這麼說。
您正在尋找'string.Format' – SLaks