代码之家  ›  专栏  ›  技术社区  ›  pistacchio

VB.NET中的多行字符串

  •  132
  • pistacchio  · 技术社区  · 16 年前

    有没有一种方法可以像Python一样在VB.NET中使用多行字符串

    a = """
    multi
    line
    string
    """
    

    还是PHP?

    $a = <<<END
    multi
    line
    string
    END;
    

    当然不是这样的

    "multi" & _
    "line
    
    21 回复  |  直到 5 年前
        1
  •  231
  •   Community CDub    5 年前

    你可以用 XML Literals 为了达到类似的效果:

    Imports System.XML
    Imports System.XML.Linq
    Imports System.Core
    
    Dim s As String = <a>Hello
    World</a>.Value
    

    请记住,如果您有特殊字符,则应使用CDATA块:

    Dim s As String = <![CDATA[Hello
    World & Space]]>.Value
    

    中引入了多行字符串文字 Visual Basic 14 (在 ).以上示例现在可以写成:

    Dim s As String = "Hello
    World & Space"
    

    MSDN article isn't updated yet (as of 2015-08-01), so check some answers below for details.

    Roslyn New-Language-Features-in-VB-14 Github存储库。

        2
  •  50
  •   JaredPar    16 年前

    VB.Net没有这样的功能,它将

    多行字符串示例

    Dim x = "line1" & vbCrlf & _
            "line2"
    

    VisualStudio2010

    Dim x = "line1" & vbCrlf & 
            "line2"
    
        3
  •  41
  •   Community CDub    12 年前

    我使用了这个变体:

         Dim query As String = <![CDATA[
            SELECT 
                a.QuestionID
            FROM 
                CR_Answers a
    
            INNER JOIN 
                CR_Class c ON c.ClassID = a.ClassID
            INNER JOIN
                CR_Questions q ON q.QuestionID = a.QuestionID
            WHERE 
                a.CourseID = 1
            AND 
                c.ActionPlan = 1
            AND q.Q_Year = '11/12'
            AND q.Q_Term <= (SELECT CurrentTerm FROM CR_Current_Term)
        ]]>.Value()
    

    它允许<&燃气轮机;串连

        4
  •  34
  •   Community CDub    5 年前

    Dim sql As String = "
        SELECT ID, Description
        FROM inventory
        ORDER BY DateAdded
    "
    

    您可以将它们与 要最大限度地提高可用性:

    Dim primaryKey As String = "ID"
    Dim inventoryTable As String = "inventory"
    
    Dim sql As String = $"
        SELECT {primaryKey}, Description
        FROM {inventoryTable}
        ORDER BY DateAdded
    "
    

    请注意,插值字符串以开头 $ 你需要照顾好我 " , { } 包含在–将其转换为 "" , {{ }}

    在这里你可以看到真实的 语法突出显示 上述代码示例的插值部分的数量:

    enter image description here

    如果您想知道Visual Studio编辑器对它们的识别是否也适用于重构(例如批量重命名变量),那么您是对的, 代码重构可以处理这些问题。 更不用说它们还支持智能感知、引用计数或代码分析。

        5
  •  20
  •   Lucian Wischik    10 年前

    Visual Basic 14.0中引入了多行字符串文字- https://roslyn.codeplex.com/discussions/571884

    您可以在VS2015预览中使用then,现在就可以使用它- http://www.visualstudio.com/en-us/downloads/visual-studio-2015-downloads-vs

    Dim multiline = "multi
    line
    string"
    

    VB字符串现在基本上与C#逐字逐句字符串相同-它们不支持像\n这样的反斜杠转义序列,并且它们允许字符串中有换行符,并且可以用双引号转义引号符号“”

        6
  •  14
  •   Adam    13 年前

    这是一篇对我很有帮助的文章,但是没有人提到如何使用它 连接 如果你想发送一些变量,99%的时候你都需要这样做。

    ... <%= %&燃气轮机;

    以下是您的操作方法:

    <SQL> SELECT * FROM MyTable WHERE FirstName='<%= EnteredName %>' </SQL>.Value

        7
  •  10
  •   roufamatic RichardJohnn    11 年前

    好吧,既然您似乎正在使用python,我建议您将文本复制到python中,比如:

     s="""this is gonna 
    last quite a 
    few lines"""
    

    然后执行以下操作:

      for i in s.split('\n'):
        print 'mySB.AppendLine("%s")' % i
    
    #    mySB.AppendLine("this is gonna")
    #    mySB.AppendLine("last quite a")
    #    mySB.AppendLine("few lines")
    

      print ' & _ \n'.join(map(lambda s: '"%s"' % s, s.split('\n')))
    
    #    "this is gonna" & _ 
    #    "last quite a" & _ 
    #    "few lines"
    

    然后至少你可以把它复制出来,放到你的VB代码中。绑定热键可获得额外积分 (最快获得: Autohotkey )对粘贴缓冲区中的任何内容执行此操作。同样的想法也适用于SQL格式化程序。

        8
  •  9
  •   windchaser    13 年前

    vb.net中使用XElement类的多行字符串文字。

    Imports System.Xml.Linq
    
    Public Sub Test()
    
    dim sOderBy as string = ""
    
    dim xe as XElement = <SQL>
                    SELECT * FROM <%= sTableName %>
                     <ORDER_BY> ORDER BY <%= sOrderBy %></ORDER_BY>
                     </SQL>
    
    '** conditionally remove a section 
    if sOrderBy.Length = 0 then xe.<ORDER BY>.Remove
    
    '** convert XElement value to a string 
    dim sSQL as String = xe.Value
    
    End Sub
    
        9
  •  7
  •   animuson    13 年前

    对我来说,这是VB作为一种语言最烦人的地方。 说真的,我曾经在一个文件中写过字符串,并编写了一些代码,大致如下:

    Dim s as String = file_get_contents("filename.txt")
    

    如果需要,我可以直接在SQL server上测试查询。

    我目前的方法是在SQL Server上使用存储过程并调用它,这样我就可以将参数传递给查询,等等

        10
  •  5
  •   Nelson    10 年前

    基本上,您必须在VB变量之前终止CDATA标记,然后在变量之后重新添加,这样CDATA就不会捕获VB代码。您需要将整个代码块包装在一个标记中,因为您将有多个CDATA块。

    Dim script As String = <code><![CDATA[
      <script type="text/javascript">
        var URL = ']]><%= domain %><![CDATA[/mypage.html';
      </script>]]>
    </code>.value
    
        11
  •  3
  •   habakuk    13 年前

     Dim a = My.Resources.Whatever_you_chose
    
        12
  •  3
  •   John Samuel Anderson    13 年前

    但是我也使用VB.Net,所以这里是我获取更可读的长字符串的捷径。

      Dim lines As String() = {
        "Line 1",
        "Line 2",
        "Line 3"
      }
      Dim s As String = Join(lines, vbCrLf)
    
        13
  •  2
  •   Jack Gajanan    11 年前

    您可以使用XML来实现这一点

    dim vrstr as string = <s>
        some words
        some words
        some
        words
    </s>
    
        14
  •  1
  •   user1166557    12 年前

    在VisualStudio2010(VB NET)中,我尝试了以下方法,效果很好

    Dim HtmlSample As String = <anything>what ever you want to type here with multiline strings</anything>
    
    dim Test1 as string =<a>onother multiline example</a>
    
        15
  •  1
  •   Bart VdA    9 年前

    在Visual Basic 14中作为Visual Studio 2015的一部分提供 https://msdn.microsoft.com/en-us/magazine/dn890368.aspx

    但尚未得到R#的支持。好消息是他们很快就会得到支持!请投票表决 Youtrack 要通知JetBrains,您也需要它们。

        16
  •  0
  •   Ori Samara    9 年前

    如果您在VB.Net中需要一个带有行代码变量的XML文本,您可以这样做:

    <Tag><%= New XCData(T.Property) %></Tag>
    
        17
  •  0
  •   ᗩИᎠЯƎᗩ    7 年前

    你也可以使用 System.Text.StringBuilder

    Dim sValue As New System.Text.StringBuilder
    sValue.AppendLine("1st Line")
    sValue.AppendLine("2nd Line")
    sValue.AppendLine("3rd Line")
    

    然后使用以下方法获取多行字符串:

    sValue.ToString()
    
        18
  •  -1
  •   Zac    9 年前

    MySql = ""
    MySql = MySql & "SELECT myTable.id"
    MySql = MySql & " FROM myTable"
    MySql = MySql & " WHERE myTable.id_equipment = " & lblId.Text
    
        19
  •  -1
  •   SimplyInk LuizLoyola    8 年前

    vbCrLf vbNewLine

    Dim str As String
    str = "First line" & vbCrLf & "Second line"
    MsgBox(str)
    str = "First line" & vbNewLine & "Second line"
    MsgBox(str)
    

    它将显示两个相同的消息框,共2行。

        20
  •  -9
  •   Jason Irwin    16 年前

    不,VB.NET还没有这样的功能。但是,它将在VB(visual basic 10)的下一次迭代中可用( link )

        21
  •  -16
  •   Andy Hohorst    16 年前

    foo = @"Multiline
    String"
    

    这对于诸如@“C:\Windows\System32\”之类的东西也很有用-它基本上关闭转义并打开多行。