Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在带有空格的字符串中插入另一个变量?C#_C#_String_Text - Fatal编程技术网

如何在带有空格的字符串中插入另一个变量?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";