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

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

Most Popular Programming Stories

More for Developers

RSS Feeds