在 C# 代码中添加换行符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1783310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding line break in C# Code behind page
提问by Shantanu Gupta
I have written a code in C# which is exceeding page width, so i want it to be broken into next line according to my formatting. I tried to search a lot to get that character for line break but was not able to find out.
我在 C# 中编写了一个超过页面宽度的代码,所以我希望根据我的格式将它分成下一行。我试图搜索很多以获得换行符的字符,但无法找到。
In VB.NET I use '_' for line break, same way what is used in C# ? I am trying to break a string.
在 VB.NET 中,我使用“_”作为换行符,与 C# 中使用的方式相同?我试图打破一个字符串。
Thanks in Advance Shantanu Gupta
提前致谢 Shantanu Gupta
采纳答案by J?rn Schou-Rode
Option A:concatenate several string literal into one:
选项 A:将几个字符串文字连接成一个:
string myText = "Looking up into the night sky is looking into infinity" +
" - distance is incomprehensible and therefore meaningless.";
Option B:use a single multiline string literal:
选项 B:使用单个多行字符串文字:
string myText = @"Looking up into the night sky is looking into infinity
- distance is incomprehensible and therefore meaningless.";
With option B, the newline character(s) will be part of the string saved into variable myText
. This might, or might not, be what you want.
使用选项 B,换行符将成为保存到变量中的字符串的一部分myText
。这可能是您想要的,也可能不是。
回答by Jake Pearson
C# doesn't have an explicit line break character. You statements end with a semicolon so you can span your statements over many lines. These are both the same:
C# 没有明确的换行符。您的语句以分号结尾,因此您可以将语句跨越多行。这两者是一样的:
public string GenerateString()
{
return "abc" + "def";
}
public string GenerateString()
{
return
"abc" +
"def";
}
回答by JaredPar
C# code can be split between lines on pretty much any syntatic construct without a need for a '_' style construct.
C# 代码可以在几乎任何语法构造上的行之间拆分,而无需“_”样式构造。
For example
例如
foo.
Bar(
42
, "again");
回答by Pat
If I am understanding this correctly, you should be able to break the string into substrings to accomplish this.
如果我正确理解这一点,您应该能够将字符串分解为子字符串来完成此操作。
i.e.:
IE:
string s = "this is a really long string" +
"and this is the rest of it";
回答by BenAlabaster
In C# there's no 'new line' character like there is in VB.NET. The end of a logical 'line' of code is denoted by a ';'. If you wish to break the line of code over multiple lines, just hit the carriage return (or if you want to programmatically add it (for programmatically generated code) insert 'Environment.NewLine' or '\r\n'.
在 C# 中没有像在 VB.NET 中那样的“换行”字符。代码的逻辑“行”的结尾用“;”表示。如果您希望将代码行分成多行,只需点击回车(或者如果您想以编程方式添加它(对于以编程方式生成的代码),请插入“Environment.NewLine”或“\r\n”。
Edit:In response to your comment: If you wish to break a string over multiple lines (i.e. programmatically), you should insert the Environment.NewLine character. This will take the environment into account in order to create the line ending. For instance, many environments, including Unix/Linux only use a NewLine character (\n), but Windows uses both carriage return and line feed (\r\n). So to break a string you would use:
编辑:响应您的评论:如果您希望将字符串分成多行(即以编程方式),您应该插入 Environment.NewLine 字符。这将考虑环境以创建行尾。例如,包括 Unix/Linux 在内的许多环境只使用换行符 (\n),但 Windows 使用回车和换行符 (\r\n)。因此,要断开字符串,您将使用:
string output = "Hello this is my string\r\nthat I want broken over multiple lines."
Of course, this would only be good for Windows, so before I get flamed for incorrect practice you should actually do this:
当然,这只会对 Windows 有好处,所以在我因不正确的练习而受到指责之前,你应该这样做:
string output = string.Format("Hello this is my string{0}that I want broken over multiple lines.", Environment.NewLine);
Or if you want to break over multiple lines in your IDE, you would do:
或者,如果您想在 IDE 中拆分多行,您可以这样做:
string output = "My string"
+ "is split over"
+ "multiple lines";
回答by Andre Haverdings
guys.. use resources for long strings in code behind!!
伙计们.. 在后面的代码中使用长字符串的资源!!
also.. you don't need an _ for codeline breaks in C#. In VB the codelines end with a newline character (or a ':'), using the the _ would tell the parser it has not reached the end of the line yet. The codeline in C# ends with a ';' so you can use newlines to styleformat your code.
也.. 在 C# 中,您不需要 _ 来表示代码行中断。在 VB 中,代码行以换行符(或“:”)结尾,使用 _ 会告诉解析器它尚未到达行尾。C# 中的代码行以“;”结尾 所以你可以使用换行符来设置你的代码的样式。
回答by Nitin Sangwan
dt = abj.getDataTable(
"select bookrecord.userid,usermaster.userName, "
+" book.bookname,bookrecord.fromdate, "
+" bookrecord.todate,bookrecord.bookstatus "
+" from book,bookrecord,usermaster "
+" where bookrecord.bookid='"+ bookId +"' "
+" and usermaster.userId=bookrecord.userid "
+" and book.bookid='"+ bookId +"'");
回答by Rikin Patel
Use @ symbol before starting the string. like
在开始字符串之前使用@ 符号。喜欢
string s = @"this is a really
long string
and this is
the rest of it";
回答by Rauld
result = "Minimum MarketData"+ Environment.NewLine
+ "Refresh interval is 1";
回答by JaySeeAre
Strings are immutable, so using
字符串是不可变的,所以使用
public string GenerateString()
{
return
"abc" +
"def";
}
will slow you performance - each of those values is a string literal which must be concatenated at runtime - bad news if you reuse the method/property/whatever alot...
会降低你的性能 - 这些值中的每一个都是一个必须在运行时连接的字符串文字 - 如果你重用方法/属性/任何东西都是坏消息......
Store your string literals in resources is a good idea...
将字符串文字存储在资源中是个好主意......
public string GenerateString()
{
return Resources.MyString;
}
That way it is localisable and the code is tidy (although performance is pretty terrible).
这样它就可以本地化并且代码很整洁(尽管性能非常糟糕)。