Shenghi Posted July 1, 2006 Share Posted July 1, 2006 (edited) Very simple, and I was actually surprised it didn't exist yet. I came to it because I happen to occasionaly use the $count identifier in mIRC scripting. _StringCount counts the number of times a substring occurs in a string. The way it works is copied from the way it works in mIRC scripting, so _StringCount("aaa","aa") returns 1. If someone thinks it would be convenient to add a way for it to return 2 in that case, you are welcome to write it :] Here's the function: expandcollapse popup;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; _StringCount("string","substring",N) ; Counts the number of times 'substring' occurs in 'string.' ; If N is 0 the function is non case-sensitive, if N is bigger ; than 0, it is. ; ; Parameters: ; $_text - holds text ; $_$count - substring ; $_N - case sensitivity, 0 or >0 ; ; Returns on success: - number of times 'substring' occurs in 'string' ; Returns on failure: - -1 ; ; @error settings: - 0: If the function was succesful and 'substring' was found ; - 1: If the function was succesful and 'substring' was not found ; - 2: If the function failed ; ; Note: Although N can be larger than 0, it can not be a negative value. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Func _StringCount($_text,$_count,$_cs = 0) Local $iNum If StringIsDigit($_cs) <> 1 Then SetError(2) Return -1 EndIf If $_cs >= 1 Then StringReplace($_text,$_count,$_count & "t",0,1) $iNum = @extended Else StringReplace($_text,$_count,$_count & "t") $iNum = @extended EndIf If $iNum = 0 Then SetError(1) Return $iNum Else SetError(0) Return $iNum EndIf ;; if the script reaches this part, obviously something ;; has gone wrong, and an error is returned SetError(2) Return -1 EndFunc ;; _StringCount Edited July 1, 2006 by Shenghi Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted July 1, 2006 Moderators Share Posted July 1, 2006 (edited) Nice job, I do something similar with string splitMsgBox(0,'Count',_StringCount('aaa', 'aa')) Func _StringCount($sString, $vDelimeter, $iEntireString = 1) If Not StringInStr($sString, $vDelimeter) Then SetError(1) Return 0 EndIf Return UBound(StringSplit($sString, $vDelimeter, $iEntireString)) - 2 EndFunc Edited July 1, 2006 by SmOke_N Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
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