VB换行逃脱char?

时间:2012-12-28 21:42:48

标签: c++ c vb.net newline output

在C中我使用"1st line 1\n2nd line"作为换行符,但是VB怎么样?我知道"1st line" & VbCrLf & "2nd line"但是它太冗长了,VB中换行符的转义字符是什么?

我想打印

1st line
2nd line

我尝试使用\n,但无论我运行编译器多少次

,它总是会输出
1st line\n2nd line

有什么想法吗?

5 个答案:

答案 0 :(得分:8)

您应该使用Environment.NewLine。在Windows上评估为CR + LF,在Unix系统上评估为LF。

VB中没有CR或LF字符的转义序列。这就是"\n"从字面上对待的原因。所以,Environment.NewLine是你的家伙。

答案 1 :(得分:3)

正如其他人所说,您应该使用对环境敏感的Environment.NewLine属性。 vbCrLf是一个旧的VB6常量,它在VB.NET中提供,以实现向后兼容性。例如,你应该使用:

"1st line" & Environment.NewLine & "2nd line"

如果你有很多行,那就太罗嗦了,你可以使用StringBuilder类,而不是:

Dim builder As New StringBuilder()
builder.AppendLine("line 1")
builder.AppendLine("line 2")
builder.AppendLine("line 3")
builder.AppendLine("line 4")

如果您特别需要CR和LF字符,则应使用ControlChars类中的常量而不是旧vbCrLf常量。

答案 2 :(得分:2)

你说VB,但用VB.NET标记。你的问题也有些奇怪。不同的操作系统实际上需要不同的换行符。你想要的是Environment.NewLine。无论运行.NET框架的操作系统如何,这都将为您提供正确的新行字符。

答案 3 :(得分:1)

我仍然使用老式的body{ background-color: white; font-family:Orbitron; color:white; } .nav-pills{ font-size: 1.7em; background-color: none; margin-bottom: 10%; } .block{ background-color: #337ab7; opacity: .7; padding:10px; width:50%; margin-right: auto; margin-left: auto; border-radius:5px; } h1{ padding:0; margin-top: 0px; font-size: 5.0em; } .btn-default{ font-size:1.7em; color:#337ab7; } .pageOne{ background: url("https://images.pexels.com/photos/477230/pexels-photo-477230.jpeg?w=1260&h=750&auto=compress&cs=tinysrgb"); background-repeat: none; background-size: cover; display: inline-block; height: 1000px; width: 100%; } /* parallax effect start */ .pageOne, .pageThree{ position: relative; opacity: .7; background-position: center; background-size: cover; background-repeat: no-repeat; background-attachment: fixed; } /* parallax effect end */ .pageTwo{ background: url("https://images.pexels.com/photos/477230/pexels-photo-477230.jpeg?w=1260&h=750&auto=compress&cs=tinysrgb"); background-repeat: none; background-size: cover; display: inline-block; height: 1000px; width: 100%; padding-top: 5%; } .pageTwoblock{ background-color: #008B8B; opacity: .7; border-radius:5px; } p{ font-size: 2.5em; } .me{ height: 850px; display:block; margin-right: auto; margin-left: auto; } .pageThree{ background: url("https://images.pexels.com/photos/477230/pexels-photo-477230.jpeg?w=1260&h=750&auto=compress&cs=tinysrgb"); background-repeat: none; background-size: cover; display: inline-block; height: 1000px; width: 100%; padding-top:5%; } .page { vertical-align: top; } 例如:

<!DOCTYPE html>

    <html>

      <head>
    <title>Daniel's Portfolio | Welcome</title>
    <link rel="stylesheet" href="CSS/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <link rel="stylesheet" href="CSS/style.css">
    <link href="https://fonts.googleapis.com/css?family=Orbitron" rel="stylesheet">
    </head>
    <body>
    <div class="pageOne page text-center">
      <ul class = "nav nav-pills">
    <li>
      <a href="#">Daniel Collins</a>
    </li>
    <li class="pull-right">
      <a href="#">Contact Me</a>
    </li>
    <li class="pull-right">
      <a href="#">Portfolio</a>
    </li>
    <li class="pull-right">
      <a href="#">About Me</a>
    </li>
    </ul>
    
    <div class="block text-center">
    <h1>Daniel's Portfolio Website</h1>
    <h2>Various Projects</h2>
    </div>
    <div class = "btnList text-center">
      <a class = "btn btn-default" href="#">Reddit</a>
      <a class = "btn btn-default" href="#">GitHub</a>
      <a class = "btn btn-default" href="#">Linkedin</a>
      <a class = "btn btn-default" href="#">Facebook</a>
    </div>
    </div>
    <div class= "pageTwo page">
    <div class= "col-md-6 pageTwoblock">
    <div class="row">
    <div class ="text-center">
          <h1>Daniel Collins</h1>
          <p>
            I’m a web developer and designer living in Jacksonville, Florida, United States. I spend my days with my hands in many
             different areas of web development from back end programming (PHP, C#, Java) to front end engineering
             (HTML, CSS, and jQuery/Javascript), digital accessibility, user experience and visual design.
          </p>
        </div>
        </div>
        </div>
        </div>
    <div class= "pageThree page">
    
    </div>
       </body>
    </html>

结果是:

chr(13)

编辑:像 Dave Doknjas 一样说,在你的情况下,"Hello" & chr(13) & "World" 可以提供这条线。

答案 4 :(得分:0)

如果您正在寻找内联转义以完成诸如在String.Format中包含换行符之类的操作,则可以包含管道或类似内容,然后在字符串末尾替换它。

String.Format(“批次:{0} |产品编号:{1} |公式:{2}”,dr.Item(“批次编号”),dr.Item(“ PartNum”),dr.Item(“公式“))。Replace(” |“,Environment.NewLine)