Monday 12 December 2011

How can we split a string into words or lines


'Parmeters
'pText - The text to be splited
'Delimiter - This is a set of characters which will be considered as delimiters.
'each character in the delimiter string will be taken as a delimiter and not the
'complete string
'IncludeDelimiter - If set to True the resulting array will contain delimiters also
'revDelimiter - If this is set to True then any character other than specified in
'delimiter are considered as delimiters
Public Function SplitEx(ByVal pText, ByVal Delimiter, ByVal IncludeDelimiter, ByVal revDelimiter)
'If a blank delimiter then split the whole string into array
'of characters
    If Delimiter = "" Then
        iCount = Len(pText)
        ReDim outarray(iCount)
       
        For i = 1 To iCount
            sChar = Mid(pText, i, 1)
            outarray(i - 1) = sChar
        Next
    Else
        'Variable to store the word
        sword = ""
       
        iCount = Len(pText)
        ReDim outarray(0)
       
        'Loop through all characters in the string
        For i = 1 To iCount
        'Get the current character
            sChar = Mid(pText, i, 1)
           
            'If the character is one of the delimiters
            'and revDelimiter flag is false then
            'create the word and store it in the result array
            If (InStr(Delimiter, sChar) <> 0 And Not revDelimiter) Then
                'store the word in the array
                If sword <> "" Then
               outarray(UBound(outarray)) = sword
               sword = ""
               'Increase the array size by 1              
               ReDim Preserve outarray(UBound(outarray) + 1)
        End If      

       'If IncludeDelimiter flag is true then add
                'the delimiter as well to the array
                If IncludeDelimiter Then
                    outarray(UBound(outarray)) = sChar
                    ReDim Preserve outarray(UBound(outarray) + 1)
                End If
            ElseIf (InStr(Delimiter, sChar) = 0 And revDelimiter) Then
            'If the character is not in the delimiter string
            'and revDelimiter is TRUE then create the word
                If sword <> "" Then
                    outarray(UBound(outarray)) = sword
                    sword = ""
                    ReDim Preserve outarray(UBound(outarray) + 1)
                End If
               
                'The delimiter word is always added to the array
                outarray(UBound(outarray)) = sChar
                ReDim Preserve outarray(UBound(outarray) + 1)
            Else
            'Continue to capture the word
                sword = sword + sChar
            End If
        Next
       
        'In case word in not blank then add to the array
        If sword <> "" Then
            outarray(UBound(outarray)) = sword
            ReDim Preserve outarray(UBound(outarray) + 1)
        End If
    End If
   
    'Decrease the array size by 1 as we started with a array
    'of length 1 when we had not data at all
    ReDim Preserve outarray(UBound(outarray) - 1)
   
    'return the array  
    SplitEx = outarray
End Function