Pages

Wednesday, December 23, 2009

Processing Strings - Miscellaneous

The Code fragment below helps you to compare the contents of two different strings and return only those contents that are present in the target string but not in the source string.

This is a very special case though.


'**********************************************************************************
'@Author : Karthikeyan A
'@Purpose : To compare the contents of two different strings and return only those contents that are present in the target string but not in the source string
'@Type : Function
'@Name : filterDuplicates
'@Param : sourceValue As String,targetValue As String
'@Return : Boolean (has operation succeded, True stands for success and False stands for failure)
'**********************************************************************************
Function filterDuplicates(sourceValue As String,targetValue As String) As Boolean

Dim tempTarget As String
Dim splitSourceValue As Variant
Dim splitTargetValue As Variant

'mark the flow of contol, entering into the current function
filterDuplicates=False

' if the sourceValue is empty then exit the function
If Not sourceValue="" Then

'declare the variables necessary for further manipulation
tempTarget=""

'split the contents of the source string and see if they exist in the target string
'if yes then replace the corresponding value in the target string with null
splitSourceValue=Split(sourceValue,",")
Forall sourceVal In splitSourceValue

If Instr(targetValue,sourceVal) Then
targetValue=Replace(targetValue,sourceVal,"")
End If
End Forall

'skip the null values in the target string which resulted in the above step and assign it back to the target string
splitTargetValue=Split(targetValue,",")
Forall targetVal In splitTargetValue
If Not Trim(targetVal)="" Then
tempTarget=tempTarget+targetVal+","
End If
End Forall
tempTarget=Strleftback(tempTarget,",")

targetValue=tempTarget
End If

'mark the flow of control,moving out of the current function
filterDuplicates=True

End Function

No comments:

Post a Comment