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

Unit 5: VB String and Operators



               ’ SearchFromRight is True, the returned string is that string to the left of the
               ’ LAST occurrance of TrimToChar. If TrimToChar is not found in the string,
               ’ the entire Text string is returned. TrimChar may be more than one character.
               ’ Comparison is done in Text mode (case does not matter).
               ’ If TrimChar is an empty string, the entire Text string is returned.
               ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’
               Dim Pos As Integer
                ‘ Test to see if TrimChar is vbNullString. If so, return the whole string. If we
                ‘ don’t test here and used InStr as in the next logic block, an empty string would
                ‘ be returned.
                If TrimChar = vbNullString Then
                TrimToChar = Text
                Exit Function
                End If
                ‘ find the position in Text of TrimChar
                If SearchFromRight = True Then
                ‘ search right-to-left
                Pos = InStrRev(Text, TrimChar, -1, vbTextCompare)
                Else
               ’ search left-to-right
                Pos = InStr(1, Text, TrimChar, vbTextCompare)
                End If
               ’ return the sub string
                If Pos > 0 Then
                TrimToChar = Left(Text, Pos - 1)
                Else
                TrimToChar = Text
                End If
               End Function


                              Perhaps the biggest bottleneck is that VB makes copies of the string data when
                              doing some of the operations. Even when you’re just reading strings (and not
                              planning to make any modifications), you can easily end up making a large
                              number of copies. The copying costs you time if string processing is an
                              intensive part of your program. Another reason is that some of the widely
                              used functions are implemented in a non-straightforward way. They may be
                              doing more work than what is required for your task. Fortunately, you can
                              often replace an advanced functions with a simpler and faster alternative.




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