Page 108 - Open Soource Technologies 304.indd
P. 108

Event Driven Programming



                          of the actual data in the string variable. It is up to you as the programmer to extract the data
                          to the left of the vbNullChar. The following function will do this for you. This is a very simple
                          function, but if you use a lot of API calls in your code, it is worthy of its own function.
                          Public Function TrimToNull(Text As String) As String

                          ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’
                          ’ TrimToNull
                          ’ This function returns the portion of Text that is to the left of the vbNullChar

                          ’ character (same as Chr(0)). Typically, this function is used with strings
                          ’ populated by Windows API procedures. It is generally not used for
                          ’ native VB Strings.
                          ’ If vbNullChar is not found, the entire Text string is returned.
                          ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’

                          Dim Pos As Integer
                          Pos = InStr(1, Text, vbNullChar)
                          If Pos > 0 Then
                          TrimToNull = Left(Text, Pos - 1)

                          Else
                          TrimToNull = Text
                          End If
                          End Function
                          With a little extra code, TrimToNull can be expanded to trim to any character or string of
                          characters.
                          5.1.8 TrimToChar

                          The following function, TrimToChar, will return the text to the left of either the first or last
                          occurrence of a specified character or string of characters.

                          Public Function TrimToChar(Text As String, TrimChar As String, _

                           Optional SearchFromRight As Boolean = False) As String
                          ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’
                          ’ TrimToChar
                          ’ This function returns the portion of the string Text that is to the left of
                          ’ TrimChar. If SearchFromRight is omitted or False, the returned string

                          ’ is that string to the left of the FIRST occurrence of TrimChar. If





                          102                    LOVELY PROFESSIONAL UNIVERSITY
   103   104   105   106   107   108   109   110   111   112   113