Results 1 to 3 of 3

Thread: Split equivalent

  1. #1

    Thread Starter
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375

    Split equivalent

    I need a way to do the work of the Split and Replace functions in VB6. I have VB5 and they arent there. Are there any APIs that do that? thanks.
    You just proved that sig advertisements work.

  2. #2
    Matthew Gates
    Guest
    Here are 2 versions of the Split function.


    VB Code:
    1. 'Author: Joacim Andersson
    2. 'Origin: -
    3. 'Purpose: Split function
    4. 'Version: VB4+
    5.  
    6. Public Function VB5Split(ByVal Expression As String, Optional ByVal Delimiter As String = " ") As Variant
    7.     Dim nPos As Long
    8.     Dim vRetVal() As Variant
    9.     Dim nCount As Long
    10.    
    11.     nCount = -1
    12.     Do
    13.         nPos = InStr(Expression, Delimiter)
    14.         If nPos Then
    15.             nCount = nCount + 1
    16.             ReDim Preserve vRetVal(nCount) As Variant
    17.             vRetVal(nCount) = Left$(Expression, nPos - 1)
    18.             Expression = Mid$(Expression, nPos + Len(Delimiter))
    19.         End If
    20.     Loop While nPos > 0
    21.     VB5Split = vRetVal
    22. End Function


    VB Code:
    1. 'Author: Aaron Young
    2. 'Origin: -
    3. 'Purpose: Split function
    4. 'Version: VB4+
    5.  
    6.  
    7. Private Function Split2(ByVal sString As String, ByVal sSeparator As String) As Variant
    8.     Dim sParts() As String
    9.     Dim lParts As Long
    10.     Dim lPos As Long
    11.    
    12.     lPos = InStr(sString, sSeparator)
    13.     While lPos
    14.         ReDim Preserve sParts(lParts)
    15.         sParts(lParts) = Left$(sString, lPos - 1)
    16.         sString = Mid$(sString, lPos + Len(sSeparator))
    17.         lPos = InStr(sString, sSeparator)
    18.         lParts = lParts + 1
    19.     Wend
    20.     If Len(sString) Then
    21.         ReDim Preserve sParts(lParts)
    22.         sParts(lParts) = sString
    23.     End If
    24.     Split2 = IIf(lParts, sParts, Array())
    25. End Function



    And here is a substitute for the Replace function.


    VB Code:
    1. Public Function Replace(sIn As String, sFind As String, sReplace As String, Optional nStart As Long = 1, Optional nCount As Long = -1, Optional bCompare As VbCompareMethod = vbBinaryCompare) As String
    2.     Dim nC As Long, nPos As Integer, sOut As String
    3.     sOut = sIn
    4.     nPos = InStr(nStart, sOut, sFind, bCompare)
    5.     If nPos = 0 Then GoTo EndFn:
    6.     Do
    7.         nC = nC + 1
    8.         sOut = Left$(sOut, nPos - 1) & sReplace & Mid$(sOut, nPos + Len(sFind))
    9.         If nCount <> -1 And nC >= nCount Then Exit Do
    10.         nPos = InStr(nStart, sOut, sFind, bCompare)
    11.     Loop While nPos > 0
    12. EndFn:
    13.     Replace = sOut
    14. End Function

  3. #3
    Megatron
    Guest
    See your post in the General VB section.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width