|
-
Aug 11th, 2010, 01:43 AM
#1
Thread Starter
Addicted Member
[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
Last edited by xavierjohn22; Aug 11th, 2010 at 02:41 AM.
-
Aug 11th, 2010, 02:08 AM
#2
Re: Better Clean String
What do you mean "only at the end"? Only at the end of each line?
-
Aug 11th, 2010, 02:40 AM
#3
Thread Starter
Addicted Member
Re: Better Clean String
 Originally Posted by dee-u
What do you mean "only at the end"? Only at the end of each line?
dee-u after the last line, i did not mention at the end of each line
at the end of the last line to be clear, sometimes it is vbtab, sometimes two vbtab, sometimes vbnewline, i wanted to clear all of those.
-
Aug 11th, 2010, 02:55 AM
#4
Re: Better Clean String
Will this fit your requirement pare?
Code:
Option Explicit
Private Function Clean_String(strText As String) As String
Dim str() As String
Dim cntback As Long
str = Split(strText, vbCrLf)
For cntback = UBound(str) To 0 Step -1
If str(cntback) = vbTab Or str(cntback) = vbNewLine Or Len(str(cntback)) = 0 Or str(cntback) = vbCr Or str(cntback) = vbLf Then
'reduce size of array to remove last invalid entry
ReDim Preserve str(UBound(str) - 1)
Else
Exit For
End If
Next
Clean_String = Join(str, vbCrLf)
End Function
Private Sub Command1_Click()
Dim s As String
'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)
s = "X" & vbNewLine
s = s & vbTab & vbNewLine
s = s & "Y" & vbNewLine
s = s & "Z" & vbNewLine
s = s & vbTab & vbNewLine
s = s & vbNewLine & vbNewLine
s = s & vbCr & vbNewLine
s = s & vbLf & vbNewLine
s = s & vbNullString & vbNewLine
MsgBox Clean_String(s)
End Sub
-
Aug 11th, 2010, 02:56 AM
#5
Re: Better Clean String
Code:
Public Function TrimSpaceTABCRLF(Text As String) As String
Dim L As Long
L = Len(Text)
Do While L
Select Case AscW(Mid$(Text, L, 1))
Case 32, 9, 13, 10
L = L - 1
Case Else
Exit Do
Loop
Loop
If L Then TrimSpaceTABCRLF = Left$(Text, L)
End Function
This keeps trimming the string from the end as long as it can find unwanted character code at the end of the checked position.
-
Aug 11th, 2010, 03:20 AM
#6
Thread Starter
Addicted Member
Re: Better Clean String
Geez huge difference, it was that short? ha ha ha.
Thanks Merri. (End Select on one Loop) Thanks again.
Bro dee-u salamat too! i dont want to count back again, he he he.
Last edited by xavierjohn22; Aug 11th, 2010 at 03:37 AM.
-
Aug 11th, 2010, 03:23 AM
#7
Re: [RESOLVED] Better Clean String
No problem. You can prefer Merri's code over mine, it is more efficient.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|