在.NET中,我可以提供\r
或\n
字符串字面值,但有一种方法可以插入类似于“换行符”特殊字符,如Environment.NewLine
静态属性吗?
在.NET中,我可以提供\r
或\n
字符串字面值,但有一种方法可以插入类似于“换行符”特殊字符,如Environment.NewLine
静态属性吗?
好的,简单的选项有:
string.Format
:
string x = string.Format("first line{0}second line", Environment.NewLine);
字符串拼接:
string x = "first line" + Environment.NewLine + "second line";
C#6及以上版本支持字符串插值:
string x = $"first line{Environment.NewLine}second line";
你可以在任何地方使用 \n,并替换为:string x = "first line\nsecond line\nthird line".Replace("\n",
Environment.NewLine);
请注意,您无法将此作为字符串常量,因为Environment.NewLine
的值只在执行时才可用。
Environment.NewLine
?相反,使用它是一个好习惯。 - abatishchevusing static System.Environment;
...
$"first line{NewLine}second line";
- izceconst string stringWithNewLine =
@"first line
second line
third line";
由于这个字符串是常量,因此在编译时完成,因此这是编译器对换行符的解释。我似乎找不到解释此行为的参考文献,但我可以证明它按预期工作。我在Windows和Ubuntu(使用Mono)上编译了这段代码,然后进行了反汇编,得到了以下结果:
如您所见,在Windows中,换行符被解释为 \r\n,而在Ubuntu中则为 \n。
var sb = new StringBuilder();
sb.Append(first);
sb.AppendLine(); // which is equal to Append(Environment.NewLine);
sb.Append(second);
return sb.ToString();
first + Environment.NewLine + second
而不是更高效且(我认为)更易读的方法呢? - Jon SkeetString.Format
会一次性生成1个字符串(但由于特定文化的连接等原因,它在内部有点慢),而字符串连接则是1个结果+1个临时的,对吗? - abatishchevString.Concatenate
,直接构建一个输出字符串(如果这些字符串是字面量,则在编译器中完成连接,如我所知)。 - Richard"a"+b+"c"+d
等)在性能上是否与单个字符串相等?还是只是转换为String.Concatenate(a,b,c,d,etc)
,对吗? - abatishchevstring.Format
。字符串连接不会产生任何临时字符串,因为编译器将调用string.Concat(first, Environment.NewLine, second)
。 - Jon Skeetstring myText =
@"<div class=""firstLine""></div>
<div class=""secondLine""></div>
<div class=""thirdLine""></div>";
string myText =
@"<div class=\"firstLine\"></div>
<div class=\"secondLine\"></div>
<div class=\"thirdLine\"></div>";
如果您真的想将换行符作为常量使用,可以这样做:
public readonly string myVar = Environment.NewLine;
在C#中,readonly关键字表示该变量只能被赋值一次。您可以在此处找到相关文档。它允许声明一个常量变量,其值直到执行时才知道。
一种更方便的在格式化字符串中添加Environment.NewLine的方法是创建一个字符串扩展方法,在普通的字符串格式化过程中,将文本中的{nl}替换为Environment.NewLine。
使用方法
" X={0} {nl} Y={1}{nl} X+Y={2}".FormatIt(1, 2, 1+2);
gives:
X=1
Y=2
X+Y=3
代码
///<summary>
/// Use "string".FormatIt(...) instead of string.Format("string, ...)
/// Use {nl} in text to insert Environment.NewLine
///</summary>
///<exception cref="ArgumentNullException">If format is null</exception>
[StringFormatMethod("format")]
public static string FormatIt(this string format, params object[] args)
{
if (format == null) throw new ArgumentNullException("format");
return string.Format(format.Replace("{nl}", Environment.NewLine), args);
}
注意
如果您想让ReSharper突出显示您的参数,请在方法上方添加以下属性:
[StringFormatMethod("format")]
这种实现显然比仅使用String.Format要低效。
也许对于这个问题感兴趣的人也会对下一个问题感兴趣: C#中的命名字符串格式化
static class MyClass
{
public const string NewLine="\n";
}
string x = "first line" + MyClass.NewLine + "second line"
Environment.NewLine
,请参考其他回答。 - Richardconst string
。 - abatishchev较新版本的.NET允许您在文本前使用$,从而可以像以下方式一样在其中使用变量:
var x = $"Line 1{Environment.NewLine}Line 2{Environment.NewLine}Line 3";
"\r\n"
连接起来,就可以在textbox
中得到下面的新行。我的示例运行正常 - string s1 = comboBox1.Text; // s1 is the variable assigned to box 1, etc.
string s2 = comboBox2.Text;
string both = s1 + "\r\n" + s2;
textBox1.Text = both;
s1
s2
。StringBuilder sb = new StringBuilder();
sb.AppendLine("Some text with line one");
sb.AppendLine("Some mpre text with line two");
MyLabel.Text = sb.ToString().Replace(Environment.NewLine, "<br />")