WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Strings

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Compare Two Strings And Create Text File with List of Differences

 Keywords: Compare Strings Text File List Differences Diff 

Question:

Is there a way to compare 2 strings such that if there is something in string 1 that's not in string 2, you can create a 3rd string that lists the differences?

Answer:

;Compare two strings and create text file with list of different words.
;Detlev Dalitz.20090402.

strString1 = "Is here or there a way to compare 2 strings like this method?"
strString2 = "Do you think about a way to compare 2 strings that lists the differences?"

; strString1 = "" ; Test1
; strString2 = "" ; Test1
; strString2 = strString1 ; Test2

arrArr1 = Arrayize (strString1, " ")
arrArr2 = Arrayize (strString2, " ")

intArr1Dim = ArrInfo (arrArr1, 1)
intArr2Dim = ArrInfo (arrArr2, 1)

arrArr3 = ArrDimension (Max (intArr1Dim, intArr2Dim), 3)

intArr1Max = intArr1Dim - 1
intArr2Max = intArr2Dim - 1

intMin = 0
intMax1 = Min (intArr1Max, intArr2Max)

For intElem = intMin To intMax1
   If arrArr1[intElem] != arrArr2[intElem]
      arrArr3[intElem, 0] = intElem + 1
      arrArr3[intElem, 1] = arrArr1[intElem]
      arrArr3[intElem, 2] = arrArr2[intElem]
   EndIf
Next

intMax2 = Max (intArr1Max, intArr2Max)
If intMax2 > intMax1
   intMin = intMax1 + 1
   If intArr1Max > intArr2Max
      For intElem = intMin To intMax2
         arrArr3[intElem, 0] = intElem + 1
         arrArr3[intElem, 1] = arrArr1[intElem]
      Next
   Else
      For intElem = intMin To intMax2
         arrArr3[intElem, 0] = intElem + 1
         arrArr3[intElem, 2] = arrArr2[intElem]
      Next
   EndIf
EndIf

strFileOut = StrCat (ItemRemove (-1, IntControl (1004, 0, 0, 0, 0), "."), ".txt")
intBytesWritten = ArrayFilePutCSV (strFileOut, arrArr3, "|", @FALSE, 2)
If intBytesWritten == 0 Then FileDelete (strFileOut)
   Else Run (strFileOut, "")

;1|Is|Do
;2|here|you
;3|or|think
;4|there|about
;11|like|that
;12|this|lists
;13|method?|the
;14||differences?

Exit

Article ID:   W18284
Filename:   Compare Two Strings And Create Text File with List of Differences.txt
File Created: 2009:04:02:08:51:50
Last Updated: 2009:04:02:08:51:50