Advertisement
jargon

basVB6ForVB5.bas

Feb 23rd, 2013
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Attribute VB_Name = "basVB6ForVB5"
  2. Option Explicit
  3.  
  4. Public Enum CompareMethod
  5.     BinaryCompare
  6.     TextCompare
  7. End Enum
  8. Public Function InStrRevVB5(ByVal StringCheck As String, ByVal StringMatch As String, Optional ByVal Start As Long = -1, Optional ByVal Compare As CompareMethod = BinaryCompare) As Long
  9.  
  10. 'StringCheck:   The string to search.
  11. 'StringMatch:   The string to find.
  12. 'Start:         -1 = search entire string. Positive number = search only up to that position.
  13. 'Compare:       The compare method (binary or text)
  14.  
  15. 'Returns:       The last position of StringMatch within StringCheck.
  16.  
  17. Dim lPos        As Long
  18. Dim lSavePos    As Long
  19.  
  20.     If Start = -1 Then Start = Len(StringCheck)
  21.    
  22.     'Find the last instance of StringMatch within StringCheck.
  23.    lPos = InStr(1, StringCheck, StringMatch, Compare)
  24.     While lPos > 0 And lPos < Start
  25.         lSavePos = lPos
  26.         lPos = InStr(lPos + 1, StringCheck, StringMatch, Compare)
  27.     Wend
  28.    
  29.     InStrRevVB5 = lSavePos
  30.        
  31. End Function
  32.  
  33. Public Function JoinVB5(SourceArray As Variant, Optional ByVal Delimiter As String = " ") As String
  34.  
  35. 'SourceArray:   The array of strings to join.
  36. 'Delimiter:     The delimiter used in the join.
  37.  
  38. Dim lIdx    As Long
  39. Dim lLower  As Long
  40. Dim lUpper  As Long
  41. Dim sRet    As String
  42.  
  43.     On Error GoTo LocalError
  44.     'Return nothing if array has no lower or upper bounds.
  45.    lLower = LBound(SourceArray)
  46.     lUpper = UBound(SourceArray)
  47.    
  48.     'Concatinate the strings.
  49.    For lIdx = lLower To lUpper
  50.         sRet = sRet & SourceArray(lIdx) & Delimiter
  51.     Next
  52.    
  53.     'Remove last delimiter.
  54.    If Len(sRet) > 0 Then
  55.         sRet = Left$(sRet, Len(sRet) - Len(Delimiter))
  56.     End If
  57.    
  58.     'Return joined strings.
  59.    JoinVB5 = sRet
  60.    
  61. NormalExit:
  62.     Exit Function
  63.  
  64. LocalError:
  65.     Resume NormalExit
  66.    
  67. End Function
  68.  
  69. Public Function SplitVB5(Expression As String, Optional ByVal Delimiter As String = "  ", Optional ByVal Limit As Long = -1, Optional ByVal Compare As CompareMethod = BinaryCompare) As Variant
  70.  
  71. 'Expression:    The string to split.
  72. 'Delimiter:     The delimiter used for the split.
  73. 'Limit:         The max number of elements to return (-1 = all elements).
  74. 'Compare:       The compare method (binary or text).
  75.  
  76. 'Returns:       A zero-based variant array of substrings or
  77. '               entire expression as element(0) if no delimiter found.
  78.  
  79. Dim lPos1   As Long
  80. Dim lPos2   As Long
  81. Dim lIdx    As Long
  82. Dim lCnt    As Long
  83. Dim saTmp() As String
  84.  
  85.     'Initialize the variables
  86.    lCnt = 0
  87.     lPos1 = 1
  88.     ReDim saTmp(99)
  89.    
  90.     'Search for the delimiter.
  91.    lPos2 = InStr(1, Expression, Delimiter, Compare)
  92.     While lPos2 > 0 And ((lCnt <= Limit) Or (Limit = -1))
  93.         'Delimiter found, extract the substring between the delimiters.
  94.        saTmp(lCnt) = Mid$(Expression, lPos1, lPos2 - lPos1)
  95.         lCnt = lCnt + 1
  96.         If (lCnt Mod 100) = 0 Then
  97.             'Increase array size if needed.
  98.            ReDim Preserve saTmp(UBound(saTmp) + 100)
  99.         End If
  100.         'Move to end of last delimiter found.
  101.        lPos1 = lPos2 + Len(Delimiter)
  102.         'Search for the next delimiter.
  103.        lPos2 = InStr(lPos1, Expression, Delimiter, Compare)
  104.     Wend
  105.    
  106.     If lPos1 < Len(Expression) Then
  107.         'Extract last substring.
  108.        saTmp(lCnt) = Mid$(Expression, lPos1)
  109.         lCnt = lCnt + 1
  110.     End If
  111.    
  112.     'Resize the array to correct size.
  113.    If lCnt > 0 Then
  114.         ReDim Preserve saTmp(lCnt - 1)
  115.     Else
  116.         ReDim saTmp(-1 To -1)
  117.     End If
  118.    
  119.     'Return the array.
  120.    SplitVB5 = saTmp
  121.    
  122. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement