Results 1 to 1 of 1

Thread: Change text to string

Threaded View

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2008
    Location
    Between the keyboard and the chair.
    Posts
    122

    Change text to string

    I know the title seems a little confusing... but here is my idea...
    Basically, as you cannot put quotes into a string without using chr(34) too easily, just for the heck of it, and to learn more, I coughed up this furball I call text2String. basically, you all the function, entering a line of text(ie from a file, or a textbox), and it will return to you the string used to write a file with. Changes ' " ' to chr(34), as well as if the input had newlines in it, the line will " & _" break, making it easier to read.

    Obviously, not too helpful, but its practice, and... I did mention it was a furball for a reason. Please help me tidy this mess up!
    Code:
    Option Explicit
    
    Private Function ConvertQuotesAndCrLf(inString As String) As String
        Dim i As Integer
        Dim currentChar As String
        Dim outString As String
        Dim lastwasChr As Boolean
        Dim lastwasCrLf As Boolean
        Dim AddAmperstand As String
        
        For i = 1 To Len(inString)
            currentChar = Mid(inString, i, 1)
            If i = 1 Then 'first char
                If currentChar = Chr(34) Then
                    outString = outString & "Chr(34) & " & Chr(34)
                ElseIf currentChar = Chr(10) Then
                    outString = outString & "Chr(10) & "
                ElseIf currentChar = Chr(13) Then
                    outString = outString & "Chr(13) & "
                Else
                    outString = Chr(34) & currentChar
                End If
            ElseIf i = Len(inString) Then  'last char
                If currentChar = Chr(34) Then
                    outString = outString & Chr(34) & " & Chr(34)"
                ElseIf currentChar = Chr(10) Then
                    If Not lastwasChr Then
                        outString = outString & Chr(34) & " & Chr(10)"
                    Else
                        outString = outString & " & Chr(10)"
                    End If
                ElseIf currentChar = Chr(13) Then
                    If Not lastwasChr Then
                        outString = outString & Chr(34) & " & Chr(13)"
                    Else
                        outString = outString & " & Chr(13)"
                    End If
                Else
                    outString = outString & currentChar & Chr(34)
                End If
            Else 'middle chars
                AddAmperstand = " & "
                If lastwasCrLf Then
                    AddAmperstand = ""
                    lastwasCrLf = False
                End If
                
                If currentChar = Chr(34) Then
                    outString = outString & Chr(34) & AddAmperstand & "Chr(34)"
                    lastwasChr = True
                ElseIf currentChar = Chr(10) Then
                    If lastwasChr Then
                        outString = outString & AddAmperstand & "Chr(10)"
                        lastwasChr = False
                    Else
                        outString = outString & Chr(34) & AddAmperstand & "Chr(10)"
                    End If
                    outString = outString & " & _" & vbCrLf
                    lastwasChr = True
                    lastwasCrLf = True
                ElseIf currentChar = Chr(13) Then
                    If lastwasChr Then
                        outString = outString & AddAmperstand & "Chr(13)"
                        lastwasChr = False
                    Else
                        outString = outString & Chr(34) & AddAmperstand & "Chr(13)"
                    End If
                    lastwasChr = True
                Else
                    If lastwasChr Then
                        outString = outString & AddAmperstand & Chr(34) & currentChar
                        lastwasChr = False
                    Else
                        'If i = 2 Then outString = outString & Chr(34)
                        outString = outString & currentChar
                    End If
                End If
            End If
        Next i
        outString = Replace$(outString, "Chr(13) & Chr(10)", "vbCrLf", , , vbTextCompare)
        ConvertQuotesAndCrLf = outString
    End Function
    
    Public Function TextToString(inString As String) As String
        Dim outString As String
        outString = ConvertQuotesAndCrLf(inString)
        TextToString = outString
    End Function
    Small note on the code: lastwasChr means if a Chr() was added, meaning we are outside of quote marks atm. lastwasCrLf means just added & _, to no need to add another &, which is what AddAmperstand is used for. hackish in a huge way, i know!
    Attached Files Attached Files
    Last edited by EntityReborn; Sep 10th, 2008 at 06:00 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width