如何在C#中的字符串的两行之间添加空格?

时间:2015-11-28 14:27:28

标签: c# string

如何在字符串的两行之间添加空格?

例如:

string words = "Twelve Million Three Hundred and Forty-Five\n Thousand Six Hundred Seventy-Eight Only"

显示为:

enter image description here

期望的结果:

enter image description here

感谢。

重新编辑:对不起Theres已经反斜杠了N,我不想换新线,我已经有了一条新线,只是我需要两条线之间的空间,我认为它被称为领先空间。

选中此项:第二个字符串位于该行之上。 enter image description here

2 个答案:

答案 0 :(得分:1)

像这样使用Environment.NewLine

string words = "Twelve Million Three Hundred and Forty-Five" + Environment.NewLine + "Thousand Six Hundred Seventy-Eight Only";

如果要在两行之间添加空行,请像这样使用Environment.NewLine两次:

string words = "Twelve Million Three Hundred and Forty-Five" + Environment.NewLine + Environment.NewLine + "Thousand Six Hundred Seventy-Eight Only";

答案 1 :(得分:1)

使用字符串格式代替' +'防止重新分配内存。

TODO:

string words = string.format("{0}{1}{2}","Twelve Million Three Hundred and Forty-Five",Environment.NewLine,"Thousand Six Hundred Seventy-Eight Only");

不是Todo:

string words = "Twelve Million Three Hundred and Forty-Five" + Environment.NewLine + "Thousand Six Hundred Seventy-Eight Only";