【发布时间】:2011-05-04 09:34:37
【问题描述】:
在 .NET 中,我可以同时提供 \r 或 \n 字符串文字,但有一种插入方式
像Environment.NewLine静态属性这样的“新行”特殊字符?
【问题讨论】:
-
问题是什么?
标签: c# .net line-breaks
在 .NET 中,我可以同时提供 \r 或 \n 字符串文字,但有一种插入方式
像Environment.NewLine静态属性这样的“新行”特殊字符?
【问题讨论】:
标签: c# .net line-breaks
如果我理解了这个问题:将"\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 在text box 中使用已定义的类型样式。
【讨论】:
这里,Environment.NewLine 不起作用。
我把一个“br/>”放在一个字符串中并工作。
例如:
ltrYourLiteral.Text = "第一行。br/>第二行。";
【讨论】:
如果你正在使用 Web 应用程序,你可以试试这个。
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 />")
【讨论】:
较新的 .net 版本允许您在文字前面使用 $,这允许您在内部使用变量,如下所示:
var x = $"Line 1{Environment.NewLine}Line 2{Environment.NewLine}Line 3";
【讨论】:
我更喜欢“pythonic方式”
List<string> lines = new List<string> {
"line1",
"line2",
String.Format("{0} - {1} | {2}",
someVar,
othervar,
thirdVar
)
};
if(foo)
lines.Add("line3");
return String.Join(Environment.NewLine, lines);
【讨论】:
如果你真的想要换行字符串作为常量,那么你可以这样做:
public readonly string myVar = Environment.NewLine;
C#中readonly关键字的用户表示这个变量只能赋值一次。你可以在上面找到文档here。它允许声明一个常量变量,其值直到执行时才知道。
【讨论】:
嗯,简单的选择是:
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?恰恰相反,使用它是一个好习惯
string 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>";
【讨论】:
另一种在格式字符串中方便放置 Environment.NewLine 的方法。 这个想法是创建字符串扩展方法,像往常一样格式化字符串,但也用 Environment.NewLine 替换文本中的 {nl}
用法
" 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("格式")]
这种实现显然不如 String.Format 高效
也许对这个问题感兴趣的人也会对下一个问题感兴趣: Named string formatting in C#
【讨论】:
如果你想要一个包含 Environment.NewLine 的 const 字符串,你可以这样做:
const string stringWithNewLine =
@"first line
second line
third line";
由于这是在 const 字符串中,它是在编译时完成的,因此它是编译器对换行符的解释。我似乎找不到解释这种行为的参考,但我可以证明它按预期工作。我在 Windows 和 Ubuntu(使用 Mono)上编译了这段代码,然后反汇编,结果如下:
如您所见,在 Windows 中换行符被解释为 \r\n 而在 Ubuntu 上被解释为 \n
【讨论】:
static class MyClass
{
public const string NewLine="\n";
}
string x = "first line" + MyClass.NewLine + "second line"
【讨论】:
Environment.NewLine——见其他答案。
const string
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?
String.Format 会一次产生 1 个字符串(但由于文化特定的连接等原因,它在内部有点慢),而字符串连接 - 1 个结果 + 1 个临时的,对吧?
String.Concatenate,它直接构建一个输出字符串(IIRC,如果字符串是文字,则在编译器中完成连接。
"a"+b+"c"+d 等)按性能是否等于单个?或者只是转换成String.Concatenate(a,b,c,d,etc),对吧?
string.Format。字符串连接不会产生任何临时字符串,因为编译器会调用string.Concat(first, Environment.NewLine, second)。