EntityReborn
Sep 10th, 2008, 05:57 AM
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!
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!
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!
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!