[RESOLVED] Better Clean String
Hi Stringers, I have this in my CS project, i search at the post here, the last part of my code from the forum search i did.
What's better way to clean this kind of string only at the end.
SAMPLE TEXT:
Valid TEXT line
< valid vbtab or vbnewline
Valid TEXT line
Valid TEXT line
Valid TEXT line
< vbnewline (to be remove)
< vbtab (to be remove)
< vbtab vbtab (to be remove)
< vbnewline (to be remove)
< empty (to be remove)
< else that is blank (to be remove)
What i am doing is expecting and removing any occurences of tab, newline or any empty line at the end. I counted backwards.
'vbtab is len=1
'newline = 0
Code:
Private Function Clean_String(strText As String) As String
Dim str() As String
Dim cntback As Long
Dim cntforward
Dim Counter As Long
Dim strFinal As String
str = Split(strText, vbCrLf)
Counter = UBound(str)
For cntback = UBound(str) To 0 Step -1
'Debug.Print Len(str(cntback))
If str(cntback) = vbTab Then
'Debug.Print "tab"
ElseIf str(cntback) = vbCrLf Then
'vbcrlf
ElseIf Len(str(cntback)) > 0 Then
'Debug.Print counter
For cntforward = 0 To Counter
strFinal = strFinal & str(cntforward) & vbCrLf
Next cntforward
Exit For
End If
Counter = Counter - 1
Next cntback
If Right(strFinal, 2) = vbNewLine Then strFinal = Left(strFinal, Len(strFinal) - 2)
If Right(strFinal, 1) = vbLf Then strFinal = Left(strFinal, Len(strFinal) - 1)
If Right(strFinal, 1) = vbCr Then strFinal = Left(strFinal, Len(strFinal) - 1)
Clean_String = strFinal
End Function
Re: [RESOLVED] Better Clean String
No problem. You can prefer Merri's code over mine, it is more efficient.