Improved Trim$ Statement

The Visual Basic Trim$() function has a serious shortcoming in that it only handles space characters and not all of the usual white spaces such as tabs, carriage returns, and line feeds.

The following functions is interchangeable with the standard Trim , but will handle all of the white spaces. In fact, if you look closely, you will discover that it can be configured to trim off any character that you would like.



public Function TrimAll(ToTrim as string) as string

    Dim Start, Finish as Integer
    Dim ToEliminate as string

    ' Base condition test
    If len(ToTrim) = 0 then
        TrimAll = ""
        Exit Function
    End If

    ' Define the characters that we want to trim off
    ToEliminate = Chr(0) & Chr(8) & Chr(9) & Chr(10) & _
                  Chr(13) & Chr(32)

    ' Find the beginning of non-blank string
    Start = 1
    While InStr(1, ToEliminate, mid$(ToTrim, Start, 1), _
                vbTextCompare) <> 0 And Start <= len(ToTrim)
        Start = Start + 1
    Wend

    ' Find the end of non-blank string
    Finish = len(ToTrim)
    While InStr(1, ToEliminate, mid$(ToTrim, Finish, 1), _
                vbTextCompare) <> 0 And Finish > 1
        Finish = Finish - 1
    Wend
    '
    ' If the string is completely blank, Start is going to be greater 
    ' than Finish
    '
    If Start > Finish then
        TrimAll = ""
        Exit Function
    End If

    ' Trim out the real contents
    TrimAll = mid$(ToTrim, Start, Finish - Start + 1)

End Function


Comments

  • Thnk you very much that;s what i want

    Posted by Legacy on 08/27/2003 12:00am

    Originally posted by: Isam Ahmed Hassan

    From my deep heart thank you very much
    

    Reply
  • A TrimAll function with very little code

    Posted by Legacy on 07/15/2003 12:00am

    Originally posted by: Brian Gillham

    Public Function TrimAll(TextIn, Optional TrimChar = " ") As String
    
    

    On Error GoTo LocalError

    TrimChar = CStr(TrimChar)
    TrimAll = CStr(TextIn)
    TrimAll = Replace(TrimAll, TrimChar, vbNullChar)
    While InStr(TrimAll, String(2, vbNullChar)) > 0
    TrimAll = Replace(TrimAll, String(2, vbNullChar), vbNullChar)
    Wend

    ' Delete Leading and Trailing
    If Left(TrimAll, 1) = vbNullChar Then TrimAll = Right(TrimAll, Len(TrimAll) - 1)
    If Right(TrimAll, 1) = vbNullChar Then TrimAll = Left(TrimAll, Len(TrimAll) - 1)

    LocalError:
    TrimAll = Replace(TrimAll, vbNullChar, TrimChar, , , vbTextCompare)
    End Function

    Reply
  • reference for Trim

    Posted by Legacy on 05/15/2003 12:00am

    Originally posted by: Maria

    I try to use one of your example but i don�t know which is the reference or the component for the function Trim. Could any one help me?
    
    Thank you,
    Maria

    Reply
  • It is what I want

    Posted by Legacy on 12/03/2002 12:00am

    Originally posted by: Maggie

    I can use it to trim the front and end blank
    characters when try to parse the commandline argument.

    Reply
  • just what i need!

    Posted by Legacy on 09/23/2002 12:00am

    Originally posted by: kiat

    thanks so much, exactly the neat little function that is truely useful.

    Reply
  • Thank Alot

    Posted by Legacy on 08/21/2002 12:00am

    Originally posted by: Witty Chip

    Can you help me ?
    I need "notepad" source code

    Reply
  • Logical Search

    Posted by Legacy on 06/18/2002 12:00am

    Originally posted by: Rodrigo Silva

    Those logical searches somehow are not so logical to me. You really helped!

    Reply
  • Thank you!

    Posted by Legacy on 12/27/2001 12:00am

    Originally posted by: Scott Whigham

    Oh this is sooo helpful! I have been trying to figure out a problem for a few hours that was driving me crazxy (SQL uploads and ASP). Turns out that this function solved everything.

    Thank you!

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

  • With 81% of employees using their phones at work, companies have stopped asking: "Is corporate data leaking from personal devices?" and started asking: "How do we effectively prevent corporate data from leaking from personal devices?" The answer has not been simple. ZixOne raises the bar on BYOD security by not allowing email data to reside on the device. In addition, Zix allows employees to maintain complete control of their personal device, therefore satisfying privacy demands of valued employees and the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds