Results 1 to 7 of 7

Thread: [RESOLVED] Better Clean String

  1. #1

    Thread Starter
    Addicted Member xavierjohn22's Avatar
    Join Date
    Oct 2006
    Location
    Approx. 4921' and 3.11" asl
    Posts
    249

    Resolved [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.

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Better Clean String

    What do you mean "only at the end"? Only at the end of each line?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3

    Thread Starter
    Addicted Member xavierjohn22's Avatar
    Join Date
    Oct 2006
    Location
    Approx. 4921' and 3.11" asl
    Posts
    249

    Re: Better Clean String

    Quote Originally Posted by dee-u View Post
    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.

  4. #4
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    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
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  5. #5
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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.

  6. #6

    Thread Starter
    Addicted Member xavierjohn22's Avatar
    Join Date
    Oct 2006
    Location
    Approx. 4921' and 3.11" asl
    Posts
    249

    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.

  7. #7
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: [RESOLVED] Better Clean String

    No problem. You can prefer Merri's code over mine, it is more efficient.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

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