StringToCode – Convert a string to the corresponding VB code

StringToCode – Convert a string to the corresponding VB code

' Put quotes around a string and double any embedded Chr$(34)'' convert all control characters into ControlChars.*** VB constants' or CHR() functions'' This function is useful, for example, when you are writing a MsgBox wizard. ' Tipically, such a wizard would let the user enter a string in a multiline ' textbox control, and would later have to convert it into a quoted string in ' order to produce the actual MsgBox code. Just putting quotes around the ' string doesn't work, because you have to account for embedded quotes and ' control characters. Function StringToCode(ByVal Source As String) As String    Dim Index As Integer    Dim result As New Text.StringBuilder()    Dim openQuotes As Boolean    Dim trailingAmpersand As Boolean    ' empty string is a special case    If Source Is Nothing OrElse Source.Length = 0 Then        Return """"""    End If    For Index = 0 To Source.Length - 1        Dim ch As Char = Source.Chars(Index)        If Not Char.IsControl(ch) Then            ' open the double quotes if necessary            If openQuotes = False Then                result.Append("""")                openQuotes = True            End If            ' append the character            result.Append(ch)            ' double embedded quotes            If ch = """" Then result.Append(ch)            trailingAmpersand = False        Else            ' it is a control character             ' close double quotes, if open            If openQuotes Then                result.Append(""" & ")                openQuotes = False            End If            Select Case Asc(ch)                Case 0                    result.Append("ControlChars.NullChar & ")                Case 13                    result.Append("ControlChars.Cr & ")                Case 10                    result.Append("ControlChars.Lf & ")                Case 9                    result.Append("ControlChars.Tab & ")                Case Else                    result.Append("Chr(")                    result.Append(Asc(ch).ToString)                    result.Append(") & ")            End Select            trailingAmpersand = True        End If    Next    ' close open quotes    If openQuotes Then        result.Append("""")    ElseIf trailingAmpersand Then        result.Remove(result.Length - 3, 3)    End If    ' convert CR+LF to a single symbolic constant and return the result    Return result.ToString.Replace("ControlChars.Cr & ControlChars.Lf", _        "ControlChars.CrLf")End Function

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes