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.:confused:
Printable View
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.:confused:
Here are 2 versions of the Split function.
VB Code:
'Author: Joacim Andersson 'Origin: - 'Purpose: Split function 'Version: VB4+ Public Function VB5Split(ByVal Expression As String, Optional ByVal Delimiter As String = " ") As Variant Dim nPos As Long Dim vRetVal() As Variant Dim nCount As Long nCount = -1 Do nPos = InStr(Expression, Delimiter) If nPos Then nCount = nCount + 1 ReDim Preserve vRetVal(nCount) As Variant vRetVal(nCount) = Left$(Expression, nPos - 1) Expression = Mid$(Expression, nPos + Len(Delimiter)) End If Loop While nPos > 0 VB5Split = vRetVal End Function
VB Code:
'Author: Aaron Young 'Origin: - 'Purpose: Split function 'Version: VB4+ Private Function Split2(ByVal sString As String, ByVal sSeparator As String) As Variant Dim sParts() As String Dim lParts As Long Dim lPos As Long lPos = InStr(sString, sSeparator) While lPos ReDim Preserve sParts(lParts) sParts(lParts) = Left$(sString, lPos - 1) sString = Mid$(sString, lPos + Len(sSeparator)) lPos = InStr(sString, sSeparator) lParts = lParts + 1 Wend If Len(sString) Then ReDim Preserve sParts(lParts) sParts(lParts) = sString End If Split2 = IIf(lParts, sParts, Array()) End Function
And here is a substitute for the Replace function.
VB Code:
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 Dim nC As Long, nPos As Integer, sOut As String sOut = sIn nPos = InStr(nStart, sOut, sFind, bCompare) If nPos = 0 Then GoTo EndFn: Do nC = nC + 1 sOut = Left$(sOut, nPos - 1) & sReplace & Mid$(sOut, nPos + Len(sFind)) If nCount <> -1 And nC >= nCount Then Exit Do nPos = InStr(nStart, sOut, sFind, bCompare) Loop While nPos > 0 EndFn: Replace = sOut End Function
See your post in the General VB section.