Jump to content

compare strings for longest exact value


John117
 Share

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
Link to comment
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
Link to comment
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
Link to comment
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
Link to comment
Share on other sites

ok heres what I have so far:

For $i = 1 To 20
    $var = StringMid("lksdf123456788888888lkjh", $i, 5)
    If StringInStr("sdfsesdfesdsd12345kskdp888ofispfi800-", $var) Then
        MsgBox(0, "", $var)
    Else
        ;nothing
    EndIf
Next

edit; wow you guys have been busy *reading

Edited by Hatcheda
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...