How to use escape sequences in strings using VB.Net

Visual Basic does not support control or special or escape sequences like “\n” ( new line ) or “\t” which forces VB programmers to use constants like ControlChars.Crlf and ConrolChars.Tab. The biggest disadvantage of this is , it makes your code ugly or in technical language less readable. So this is the way we use it without escape characters

‘[ Visual Basic ]

Dim mystring as String = “Ashish Mohta ” & ControlChars.Crlf & “Phone” & ConrolChars.Tab & “Mynumber”

However there is a way to use the special characters which comes with little expense by using Regex.Unescape static method which takes a string that contains C# like escape sequences such as new line or tab.

‘[ Visual Basic ]

Dim mystring as String = Regex.Unescape(”Ashish Mohta\n Phone\tMynumber”)

I can bet that’s going to improve Vb programmers life a little bit more.

3 COMMENTS

  1. Hello,
    sir,
    My name is Ankur.
    I have to solve a pattern in Vb.Net.
    So I need to know the Escape Sequence i.e. “n” new line.
    Please guide me…..

  2. I have to tell you, I was just browsing around and found this and I love it!!! I mostly write in C# but I have to work in VB.NET for some of the older projects and this just makes my life so much easier. Thanks!!!

  3. This can be converted to an extension method as follows:

    Module Extension Methods

    Public Function Unescape(text As String) As String
    Return System.Text.RegularExpressions.Regex.Unescape(text)
    End Function
    End Module

    Then it can be used as easily as: “these are\nseveral\nlines\nof\ntext”.Unescape()

LEAVE A REPLY

Please enter your comment!
Please enter your name here