sohfeyr Posted October 8, 2006 Posted October 8, 2006 StringInStr() is good for finding if and where one string occurs in another, but what if you want to know how many times the substring occurs? These are two different functions that do the same thing. Both are very simple, but the contrast could be good for a tutorial or something. The second is more elegant, but the first would be easier to understand for someone who is new to AutoIt. ConsoleWrite(_StringCount("123123412345","2") & @crlf) ConsoleWrite(_StringCount2("123123412345","2") & @crlf) Func _StringCount($myString, $SearchFor, $casesense=0) Local $max = StringLen($myString) Local $len = StringLen($SearchFor) Local $count = 0 If $casesense=0 Then $myString = StringUpper($myString) $SearchFor = StringUpper($SearchFor) EndIf For $x = 1 to $max-$len+1 If StringMid($myString,$x,$len) = $SearchFor then $count += 1 EndIf Next Return $count EndFunc Func _StringCount2($myString, $SearchFor, $casesense=0) Local $count = 0 While StringInStr ($myString, $SearchFor, $casesense, $count+1) > 0 $count += 1 WEnd Return $count EndFunc Mine:Time Functions - Manipulate the system clock! | WinControlList (WinGetClassList++) | .Net Setup Wrapper, Detect or install .Net | Writing and using a VB .NET COM object in AutoItNot mine, but highly recommended:AutoItTreeViewExtension plugin | Menu code | Callback helper dll | Auto3Lib - Control the uncontrollable | Creating COM objects in AutoIt | Using .Net framework classes in AutoIt
/dev/null Posted October 8, 2006 Posted October 8, 2006 (edited) here's also an elegant way Func _StringCount($myString, $SearchFor, $casesense=0) local $text = StringReplace($myString, $SearchFor, "-",0,$casesense) return @extended EndFunc Almost direct from Help file of StringReplace(). Cheers Kurt Edited October 8, 2006 by /dev/null __________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *
sohfeyr Posted October 8, 2006 Author Posted October 8, 2006 (edited) here's also an elegant wayIndeed, nice example! Edited October 8, 2006 by sohfeyr Mine:Time Functions - Manipulate the system clock! | WinControlList (WinGetClassList++) | .Net Setup Wrapper, Detect or install .Net | Writing and using a VB .NET COM object in AutoItNot mine, but highly recommended:AutoItTreeViewExtension plugin | Menu code | Callback helper dll | Auto3Lib - Control the uncontrollable | Creating COM objects in AutoIt | Using .Net framework classes in AutoIt
Kickassjoe Posted October 9, 2006 Posted October 9, 2006 here's also an elegant way ph34r.gifCODE: AutoItFunc _StringCount($myString, $SearchFor, $casesense=0) local $text = StringReplace($myString, $SearchFor, "-",0,$casesense) return @extendedEndFunc Almost direct from Help file of StringReplace().CheersKurtThat is exactly what I was thinking! What goes around comes around... Payback's a bitch.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now