在C#Code后面的页面中添加换行符

Sha*_*pta 15 c#

我用C#编写了一个超出页面宽度的代码,所以我想根据我的格式将它分成下一行.我试图搜索很多东西来换行,但是没能找到.

在VB.NET中,我使用'_'表示换行符,与C#中使用的方法相同?我想破坏一个字符串.

在此先感谢Shantanu Gupta

Ben*_*ter 30

在C#中,没有像VB.NET那样的"新行"字符.代码的逻辑"行"的结尾用';'表示.如果你希望在多行上打破代码行,只需点击回车符(或者如果你想以编程方式添加它(对于以编程方式生成的代码),请插入'Environment.NewLine'或'\ r \n'.

编辑:响应您的注释:如果您希望在多行(即以编程方式)中断字符串,则应插入Environment.NewLine字符.这将考虑环境以创建行结束.例如,许多环境(包括Unix/Linux)仅使用NewLine字符(\n),但Windows同时使用回车符和换行符(\ r \n).所以要打破一个字符串,你会使用:

string output = "Hello this is my string\r\nthat I want broken over multiple lines."
Run Code Online (Sandbox Code Playgroud)

当然,这只对Windows有利,所以在我因为不正确的练习而受到抨击之前你应该这样做:

string output = string.Format("Hello this is my string{0}that I want broken over multiple lines.", Environment.NewLine);
Run Code Online (Sandbox Code Playgroud)

或者,如果要在IDE中拆分多行,则可以执行以下操作:

string output = "My string"
              + "is split over"
              + "multiple lines";
Run Code Online (Sandbox Code Playgroud)


Jør*_*ode 18

选项A:将几个字符串文字连接成一个:

string myText = "Looking up into the night sky is looking into infinity" +
    " - distance is incomprehensible and therefore meaningless.";
Run Code Online (Sandbox Code Playgroud)

选项B:使用单个多行字符串文字:

string myText = @"Looking up into the night sky is looking into infinity
- distance is incomprehensible and therefore meaningless.";
Run Code Online (Sandbox Code Playgroud)

使用选项B,换行符将成为保存到变量中的字符串的一部分myText.这可能是,也可能不是,你想要的.


Rik*_*tel 6

在启动字符串之前使用@符号.喜欢

string s = @"this is a really
long string
and this is 
the rest of it";
Run Code Online (Sandbox Code Playgroud)


Rau*_*uld 6

 result = "Minimum MarketData"+ Environment.NewLine
           + "Refresh interval is 1";
Run Code Online (Sandbox Code Playgroud)


Pat*_*Pat 5

如果我正确地理解了这一点,你应该能够将字符串分解为子串以实现此目的.

即:

string s = "this is a really long string" +
"and this is the rest of it";
Run Code Online (Sandbox Code Playgroud)