Sign in to follow this  
Followers 0
Datenshi

_StringDiff()

2 posts in this topic

#1 ·  Posted (edited)

Don't know if this already existed but i couldn't find it so, i just made my own function. Should be included to the String UDF.

I'm sure it can be made a bit better and some error checking is still missing, but I'll fix that if people would like this to be included or not.

; #FUNCTION# ;===============================================================================
;
; Name...........: _StringDiff
; Description ...: Returns the difference between 2 strings, expressed in the type requested
; Syntax.........: _StringDiff($sFirst,$sSecond[,$iStart = 1][,$iReturn = 0])
; Parameters ....: $sFirst - First String
;                  $sSecond - String to match to $sFirst, this is the string which is checked for differences.
;                  $iReturn - One of the following:
;                  |0 - Returns character that differs (Default)
;                  |1 - Returns position of character that differs
;                  $iStart - Where to start matching in the first string
;                  |1 to max length of first string. See StringMid() in help
; Return values .: Success - Returns first found difference between 2 strings,
;                  |either position or char depending on $iReturn
;                  Failure - Returns 0 and Sets @Error:
;                  |0 - No error.
;                  |1 - Start pos higher/lower then string length.
;                  |2 - No Difference found.
; Author ........: Emanuel "Datenshi" Lindgren
; Example .......; No
;
; ;==========================================================================================
Func _StringDiff($sFirst, $sSecond, $iStart = 1, $iReturn = 0)
    If StringLen($sFirst) < $iStart Or $iStart < 1 Then SetError(1)
    For $i = $iStart To StringLen($sFirst)
        If StringMid($sFirst, $i, 1) <> StringMid($sSecond, $i, 1) Then
            Switch $iReturn
                Case 0
                    Return StringMid($sSecond, $i, 1)
                Case 1
                    Return $i
            EndSwitch
        EndIf
    Next
    SetError(2)
EndFunc   ;==>_StringDiff

_StringDiff.au3

Edited by Datenshi

Share this post


Link to post
Share on other sites



If might be more useful to compare the strings for how similar they are e.g. "stationary" and "stationery". See my original post here: http://www.autoitscript.com/forum/index.php?showtopic=40843&view=findpost&p=642358 for an implementation of Levenshtein.

WBD

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0