Results 1 to 8 of 8

Thread: Splite function not avalable in VB5

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2001
    Location
    Bahrain
    Posts
    25

    Splite function not avalable in VB5

    Split(StringToSplit[, DelimiterToUse, HowMany])
    - Returns a one dimensional array
    - StringToSplit is any valid string
    - DelimiterToUse is the character that you want the string to be split by.
    - HowMany is the number of elements you want to limit the array to.
    - DelimiterToUse and HowMany are optional parameters.
    - If you leave out the DelimterToUse, the function uses a space to split the string.

    Example:

    Dim MyString, MyArray
    MyString = "apples,oranges,grapes"
    MyArray = Split(MyString, ",")


    Result:
    A one dimensional array called MyArray with 3 elements.

    Is there same this function (Split) in the VB5
    Jaffar

  2. #2
    jim mcnamara
    Guest
    No, Split() is new with VB 6.0, not found in VB 5.0. There are several functions that were added to VB 6.0 - this is one of the reasons why the forum asks you for your VB version and shows it as part of your profile- so the answers you get match your version of VB.

    Try:
    Code:
    'Here's one of a dozen versions of Split()
    
    Sub Split(ByVal arr() As String,ByVal strInput as String, ByVal strDelimit as String)
              Dim i, tmp
              Redim arr(0)
              For i = 1 to len(strInput)
                   tmp = mid(strInput,i,1)
                   if tmp = strDelimit then
                          Redim Preserve arr(Ubound(arr) + 1)
                          arr(UBound(arr) ) = ""
                   else
                          arr(Ubound(arr)) = arr(Ubound(arr)) & tmp
                   End If
              Next i
    End Sub

  3. #3
    Matthew Gates
    Guest
    Here's another one, great code from Aaron Young.


    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

  4. #4
    Addicted Member bbosh's Avatar
    Join Date
    Oct 2000
    Location
    Hell (AKA New Mexico)
    Posts
    186
    Yes, those of us who don't need the fluff of VB6 (J/K) have it hard sometimes. Most apparent is the absense of Replace and the mentioned Split. Attached is a file I use for VB6 source called VB6Compatibility.bas. Just add it to a project and you have instant Split and Replace!

    BTW, I didn't write the code. I came accross Replace here on VB Forums and Split on My Favorite Function 3 from VB World.
    Attached Files Attached Files
    Brian
    Programming: VB5 Pro (SP3) - QBasic 1.1,4.5
    Internet: HTML 4, CSS, JavaScript
    Visit AltInt.com!

  5. #5
    Matthew Gates
    Guest
    Here's the Join function for VB5 if you need it as well.
    Or to add to that module above .


    VB Code:
    1. Public Function Join(SourceArray, Optional Delimiter) As String
    2.     If IsMissing(Delimiter) Then Delimiter = ""
    3.     Dim sTemp As String
    4.     Dim nIndex As Long
    5.     If InStr(LCase(TypeName(SourceArray)), "()") Then
    6.         For nIndex = LBound(SourceArray) To UBound(SourceArray)
    7.             sTemp = sTemp & SourceArray(nIndex) & Delimiter
    8.         Next
    9.     End If
    10.     Join = Left(sTemp, Len(sTemp) - Len(Delimiter))
    11. End Function

    And the InstrRev function.


    VB Code:
    1. Public Function InStrRev(StringCheck As String, StringMatch As String, Optional Start As Long = -1) As Long
    2.     Dim sTemp As String
    3.     Dim nChar As Long
    4.     Dim nPos As Long
    5.    
    6.     sTemp = Space(Len(StringCheck))
    7.     For nChar = 0 To Len(StringCheck) - 1
    8.         Mid(sTemp, nChar + 1, 1) = Mid(StringCheck, Len(StringCheck) - nChar, 1)
    9.     Next
    10.     If Start <= Len(StringCheck) Then
    11.         nPos = InStr(IIf(Start = -1, 1, Len(StringCheck) + 1 - Start), sTemp, StringMatch)
    12.     End If
    13.     InStrRev = IIf(nPos, (Len(StringCheck) + 1) - nPos, 0)
    14. End Function


    Another InstrRev function written by Iain.


    VB Code:
    1. 'Author: Iain17
    2. 'Origin: String Manipulation
    3. 'Purpose: InStrRev for VB5
    4. 'VB version: VB4/VB5/VB6
    5. 'Comments:  Remove the Optional iStart As Long in order for this to work in VB4.
    6.  
    7.  
    8. Private Function InStrRev(strStringToSearch As String, strFind As String, Optional iStart As Long) As Long
    9.     Dim ip1 As Long, ip2 As Long
    10.     Dim iLenStringToSearch As Long
    11.     Dim iLenFind As Long
    12.    
    13.     'get the length of the string
    14.     iLenStringToSearch = Len(strStringToSearch)
    15.     iLenFind = Len(strFind)
    16.    
    17.     'if the start is 0 then set the start to the length
    18.     'of the string
    19.     If iStart = 0 Then
    20.       iStart = iLenStringToSearch
    21.     End If
    22.     iStart = iStart + 1
    23.    
    24.     ip1 = 1
    25.     Do
    26.       ip2 = InStr(ip1, strStringToSearch, strFind)
    27.       If (ip2 > 0) And (ip2 + iLenFind <= iStart) Then
    28.         'if ip2 is not zero and it is less than the
    29.         'place to start searching + the length of the
    30.         'find string then set the function
    31.         'to return that position
    32.         InStrRev = ip2
    33.       ElseIf ip2 = 0 Then
    34.         ip2 = iLenStringToSearch
    35.       End If
    36.       'set the next position to seracf from
    37.       ip1 = ip2 + 1
    38.     Loop Until ip1 >= iStart
    39.    
    40. End Function


    And a StrReverse function, written by Iain as well, I believe.


    VB Code:
    1. Private Function StrReverse(stString As String) As String
    2.     Dim i As Integer
    3.     For i = Len(stString) To 1 Step -1
    4.       StrReverse = StrReverse & Mid$(stString, i, 1)
    5.     Next i
    6. End Function

  6. #6
    Addicted Member bbosh's Avatar
    Join Date
    Oct 2000
    Location
    Hell (AKA New Mexico)
    Posts
    186
    I assume the above compatibility code has the same syntax as the true VB6 function, right?
    Brian
    Programming: VB5 Pro (SP3) - QBasic 1.1,4.5
    Internet: HTML 4, CSS, JavaScript
    Visit AltInt.com!

  7. #7
    Lively Member
    Join Date
    Mar 2000
    Posts
    87
    I was looking for this the other day, and found a really good site with optimised code. Definately worth checking out.

    http://www.xbeat.net/vbspeed/

  8. #8
    Addicted Member bbosh's Avatar
    Join Date
    Oct 2000
    Location
    Hell (AKA New Mexico)
    Posts
    186
    Great site!

    Here's the new and improved VB6Compatibility.bas file! According to VB Speed, these are (in general) the fastest code segments available. There are only a few reasons why the fastest wouldn't be contained in here:
    1) Requires a TLB. Reason: I don't like 'em.
    2) Requires an External class. That defeats the purpose of having a drop in file code module.
    3) Lenth of code. A 10k function that goes 2x as fast as a 1k module doesn't quite seem like a good trade off to me.

    Anyway, here it is. VB6Compatibility.1

    Note, both original functions have been replaced with faster code.
    Attached Files Attached Files
    Brian
    Programming: VB5 Pro (SP3) - QBasic 1.1,4.5
    Internet: HTML 4, CSS, JavaScript
    Visit AltInt.com!

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