Richard
Thu Jan 26 13:07:03 CST 2006
Sunny wrote:
> I have a function A which returns "1/23/2006-1/24/2006 1/24/2006-1/25/2006
> 1/25/2006-1/26/2006"
> Function B returns "1/24/2006-1/25/2006 1/25/2006-1/26/2006"
>
> How can I return just the difference from the 2 functions, like in this
> case I want to return "1/23/2006-1/24/2006 "?
>
> Any help is appreciated.
Hi,
My solution is a bit crude, but I can't think of another way. I parse each
string into arrays, using blank spaces as the delimiter between elements. I
then enumerate the first array to find elements missing from the second. I
repeat by enumerating the second array to find elements missing from the
first. I did not want to assume either string was more complete than the
other. My solution:
Option Explicit
Dim strValue1, strValue2, strResult
strValue1 = "1/23/2006-1/24/2006 1/24/2006-1/25/2006 1/25/2006-1/26/2006"
strValue2 = "1/24/2006-1/25/2006 1/25/2006-1/26/2006"
strResult = FindDiff(strValue1, strValue2)
Wscript.Echo "1 = " & strValue1 _
& vbCrLf & "2 = " & strValue2 _
& vbCrLf & "Result = " & strResult
Function FindDiff(strString1, strString2)
' Function to return elements of either string
' that are not in the other.
Dim arrString1, arrString2, j, k, blnMatch
arrString1 = Split(strString1, " ")
arrString2 = Split(strString2, " ")
FindDiff = ""
For j = 0 To UBound(arrString1)
blnMatch = False
For k = 0 To UBound(arrString2)
If (arrString1(j) = arrString2(k)) Then
blnMatch = True
Exit For
End If
Next
If (blnMatch = False) Then
If (FindDiff = "") Then
FindDiff = arrString1(j)
Else
FindDiff = FindDiff & " " & arrString1(j)
End If
End If
Next
For k = 0 To UBound(arrString2)
blnMatch = False
For j = 0 To UBound(arrString1)
If (arrString1(j) = arrString2(k)) Then
blnMatch = True
Exit For
End If
Next
If (blnMatch = False) Then
If (FindDiff = "") Then
FindDiff = arrString2(k)
Else
FindDiff = FindDiff & " " & arrString2(k)
End If
End If
Next
End Function
--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab -
http://www.rlmueller.net