Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: VB5,VB6
Expertise: Intermediate
Jul 8, 2002



Building the Right Environment to Support AI, Machine Learning and Deep Learning

SplitWithQualifiers - An improved version of the Split function

' A variant of the Split function, that offers the following improvements
'   You can pass text qualifier as the third argument and optionally specify
'   to treat consecutive occurrences of delimiters as one
'   For example, the following source text contains comma inside one of the 
' fields, so the
'   regular Split function will parse it incorrectly:
'   "Bayazitov","Ernest","2500 Data Drive, 100A, Sacramento, CA 95630",,,,10,,2,
' ,,
'   Also, it has some empty fields, which do not have any values or text 
' qualifiers.
'   In this case, we need to specify comma as delimiter and double quote 
' character As Text
'   qualifier, and do not treat consecutive occurrences of delimiters as one
'   Usage:
'   Dim res() As String
'   res() = SplitWithQualifiers("""Bayazitov"",""Ernest"",""2500 Data Drive, 
' 100A, Sacramento, CA 95630"",,,,10,,2,,,", ",", """", False)

Public Function SplitWithQualifiers(ByVal SourceText As String, _
    ByVal TextDelimiter As String, ByVal TextQualifier As String, _
    Optional ByVal TreatMultipleDelimitersAsOne As Boolean) As String()
    Dim strTemp() As String, strRes() As String, I As Long, J As Long, _
        A As strng, B As String, blnStart As Boolean

    If TreatMultipleDelimitersAsOne Then
        Do While (InStr(1, SourceText, TextDelimiter & TextDelimiter) And (I < _
            SourceText = Replace(SourceText, TextDelimiter & TextDelimiter, _
            I = I + 1
    End If
    If TextDelimiter <> " " Then SourceText = Trim$(SourceText)
    strTemp() = Split(SourceText, TextDelimiter)
    For I = 0 To UBound(strTemp)
        J = InStr(1, strTemp(I), TextQualifier, vbTextCompare)
        If J Then
            A = Replace(strTemp(I), TextQualifier, "")
            Select Case strTemp(I)
                Case TextQualifier & A & TextQualifier '  "xxx"
                    B = B & A & vbCrLf
                    blnStart = False
                Case TextQualifier & A     '   "xxx
                    B = B & A & TextDelimiter
                    blnStart = True
                Case A       '  xxx
                    B = B & A & TextDelimiter
                    blnStart = False
                Case A & TextQualifier   '   xxx"
                    B = B & A & vbCrLf
                    blnStart = False
            End Select
            If blnStart Then
                B = B & strTemp(I) & TextDelimiter
                B = B & strTemp(I) & vbCrLf
            End If
        End If
    Next I
    If B <> "" Then
        B = Left$(B, Len(B) - 2)
        strRes() = Split(B, vbCrLf)
        ReDim strRes(0)
        strRes(0) = SourceText
    End If
    SplitWithQualifiers = strRes()
End Function
ernest bayazitov
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date