Req3: multiline strings


[This post is part of a series, “wish-list for future versions of VB“. Just to note, I’ve been reading everyone’s comments — thank you everyone for the feedback — but I want to finish putting up the entire list before I get round to responding.]


 


IDEA: Multiline strings. VB should allow multiline strings:


        Dim s = @”This is a multiline string


which can include line-breaks


and\ttab\nescape character and < and > and &”



 


Note VB already has other ways to write multiline strings:


        Dim t = <![CDATA[this is a multiline string


which can include line-breaks


not escape characters but it can do < and > and &]]>.Value


 


        Dim u = <xml>this is a multiline string


which can include line-breaks


and&#9;tab&#13;&#10;escape characters but not &lt; or &gt; or &amp;</xml>.Value


 


Provisional evaluation from VB team: This is a decent idea, worth considering against the other decent ideas.

Comments (7)

  1. Dzonny says:

    +1

    I like the idea of multiline strings, and I also like idea of using C#-like escape sequences. I’m sick of

    "aaa" & vbCrLf & "bbb"

  2. Joshua Frank says:

    Yes!  I need this ALL THE TIME.  For instance, I often have some complicated regex pattern, and I break it into multiple lines, so I can annotate each line with a comment to explain what it does.  Currently, I put these into resource files, but this separates the regex from the place where it’s used, which is annoying.  But it’s inconceivable to use the current VB syntax to place it inline, because of this kind of thing:

    Dim sPattern = "blah" & vbCrlf & _

    "blah" & vbCrlf & _

    "blah"

    Losing the underscores is nice, but we also need to lose the & and vbCrlf stuff too.

  3. Matt Higginbotham says:

    +1 from me!!  

    I was stoked to see that they actually thought it was a good idea!

    Would love to see this feature in the future!!

  4. Kevin Ryall says:

    This would be SO useful. I already use the XML element trick all over the place for embedding text I want to preserve multi-line formatting for – primarily SQL, but also simple text templates. It would great to be able to do this natively without a hack – which works, but is less intuitive (and I’m not sure if it affects performance).

  5. Kyralessa says:

    I like the idea OK, but I too use the XML trick to do this.  So I’d call it a nice-to-have, but far from essential.

  6. r_ex says:

    We don’t need this feature.

    We can just use an extension method called ‘Unescape’ to do that.

    Dim s = "This is arnmultiline string".Unescape

  7. Richard says:

    Worth considering – easier to write and more visually pleasing than the xml and cdata examples.