如何在带有空格的字符串中插入另一个变量?C#
我知道这是一个非常愚蠢和基本的问题,但我真的不知道其他方法来实现这一点 现在我知道我可以像下面的例子一样在文本中插入空格如何在带有空格的字符串中插入另一个变量?C#,c#,string,text,C#,String,Text,我知道这是一个非常愚蠢和基本的问题,但我真的不知道其他方法来实现这一点 现在我知道我可以像下面的例子一样在文本中插入空格 int num = some numerical value; status.Text = "Successfully Deleted"+ " " + num + " " + "Files"; 然而,我想知道有没有更整洁的方法来做到这一点。我总是这样做,但我想知道是否有更简单/更整洁的方法。谢谢 使用字符串类: int num = 5; status.Text = Stri
int num = some numerical value;
status.Text = "Successfully Deleted"+ " " + num + " " + "Files";
然而,我想知道有没有更整洁的方法来做到这一点。我总是这样做,但我想知道是否有更简单/更整洁的方法。谢谢 使用字符串类:
int num = 5;
status.Text = String.Format("Successfully Deleted {0} Files", num);
使用StringBuilder类:
int num = 5;
StringBuilder sb = new StringBuilder();
sb.AppendFormat("Successfully Deleted {0} Files", num);
status.Text = sb.ToString();
更多选择:
status.Text = "Successfully Deleted " + num + " Files";
或:
status.Text=String.Format(“已成功删除{0}文件{1}”,num,(num>1)?“s”:“”);哇,那很简单,谢谢。我隐约记得这和{0}有关。谢谢。@Movieboy如果这个答案对你有帮助-不要害羞地接受它为什么在这里使用stringbuilder?它没有添加任何东西。为了不同的口味。
public static string SurroundWithSpaces(this object o)
{
return " " + o + " ";
}
status.Text = "Successfully Deleted" + num.SurroundWithSpaces() + "Files";