Jump to content
Sign in to follow this  
John117

compare strings for longest exact value

Recommended Posts

suppose I have two strings $String1 and $String2

AutoIt$String1 = lksdf123456788888888lkjh
$String2 = sdfsesdfesdsd12345kskdp888ofispfi800-oÝ÷ Ø(ºWeG­¡Ú,¢g­)à)¶¬jëh×6AutoIt$result = StringCompare( $String1,  $String2)
And have the $result be the largest section that is the sameoÝ÷ Ú'ºÚ"µÍ]]Ò] ÌÍÔÝ[HLÍ

Can this be done?

and how would it know that '888' that comes later doesn't overrule the first? or what if 888 was longer? would it then overrule the first?

Edited by Hatcheda

Share this post


Link to post
Share on other sites

the only way that I can think to do this is to take string1 digit by digit and check string2 for that digit. then the digit after string1 digit one against string two . . .

then start all over with string 1 digit 2 . . . you guys thinking of anyting faster?

since the actual string is hundreds of digits I guess I could start with matches of more than 1

sorry the first post is messy, it breaks when i try to edit it.

Edit; on second thought I am thinking work backwards until the first match so that it runs the shortest

maybe start with 20 characters? -ofcourse this method would still be really slow.

Edited by Hatcheda

Share this post


Link to post
Share on other sites

Whos your daddy? (Its probably not me unless you are age 10 or under)

#include <Array.au3>

$String1 = "lksdf123456788888888lkjh"
$String2 = "sdfsesdfesdsd12345kskdp888ofispfi800-"

$result = StringFindLongestMatch($String1, $String2)
MsgBox(0,"",$result);Returns 12345

$String1 = "abcdefghijklmnopqrstuvwxyz"
$String2 = "aaaaaaaaaaaajklmnobbbbbbbbbbbbb"

$result = StringFindLongestMatch($String1, $String2)
MsgBox(0,"",$result) ;Returns jklmno

Func StringFindLongestMatch($SFLM1, $SFLM2)
    $longestMatch = ""
    
    $array1 = StringSplit($SFLM1, "")
    $array2 = StringSplit($SFLM2, "")
    
    For $X = 1 to $array1[0]
        For $Y = $X to $array1[0]
            $tempString = _ArrayToString ($array1, "", $X, $Y)
            ConsoleWrite($tempString & @CRLF)
            
            If StringInStr($SFLM2,$tempString) AND StringLen ($tempString) > StringLen ($longestMatch ) Then $longestMatch = $tempString
        Next
    Next
    
    Return $longestMatch
EndFunc
Edited by weaponx

Share this post


Link to post
Share on other sites

Haha, that's really brilliant weaponx! I was doing it in a different way but I was stuck! Great job.

Share this post


Link to post
Share on other sites

Here is another version that doesn't require arrays (eliminating need for Array.au3):

Func StringFindLongestMatch2($SFLM1, $SFLM2)
    $longestMatch = ""
    
    For $X = 1 to StringLen ($SFLM1) 
        For $Y = $X to StringLen ($SFLM1)
            
            $tempString = StringMid ($SFLM1, $X, $Y - $X)
            ConsoleWrite($tempString & @CRLF)
            
            If StringInStr($SFLM2,$tempString) AND StringLen ($tempString) > StringLen ($longestMatch ) Then $longestMatch = $tempString
        Next
    Next
    
    Return $longestMatch
EndFunc

Share this post


Link to post
Share on other sites

Whos your daddy? (Its probably not me unless you are age 10 or under)

Kudos to you! I was racking my brain to think how to go about that. That was right on the money. :)

Share this post


Link to post
Share on other sites

hey yours looks a little better than mine :) sorry for the long delay in response, trying to think, code, and read while dodging my bouncing 3 year old and attending to my crying 9mth old :) Thanks a ton! ^_^

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  

×
×
  • Create New...