magaf Posted September 2, 2004 Share Posted September 2, 2004 how can i know the number of times a string is contained in different places in another string? Link to comment Share on other sites More sharing options...
magaf Posted September 2, 2004 Author Share Posted September 2, 2004 (edited) ahh i found a little solution i can use StringSplit and read the arrays [0] place but isnt there something more specific? also i found more not specific solutions.. Edited September 2, 2004 by magaf Link to comment Share on other sites More sharing options...
SlimShady Posted September 2, 2004 Share Posted September 2, 2004 A UDF I made some time ago. Tell me what you think of it... ;=============================================================================== ; ; Description: Count how many times a substring matches in a string. ; Syntax: CountSubs($String, $Subs) ; Parameter(s): $String - String to use ; $Subs - Substring to use ; ; Requirement(s): none ; Return Value(s): On succes - Returns the number of times the substring matched in the string. ; On failure - Return 0 if it did not match ; ; Author(s): "SlimShady" ; Note(s): none ; ;=============================================================================== Func CountSubs ($String, $Subs) Local $num = 0 While 1 $NewString = StringReplace($String, $Subs, "", 1) If $NewString == $String Then Return $num Else $String = $NewString $num = $num + 1 EndIf Wend EndFunc ;==>CountSubs Link to comment Share on other sites More sharing options...
CyberSlug Posted September 2, 2004 Share Posted September 2, 2004 SlimShady, MsgBox(4096,"", CountSubs ("aaa", "aa")) Returns 1 when it should, arguably, return 2... ; Count the number of times substring occurs in string Func _StringCount($string, $substring) Local $i, $count = 0 For $i = 1 to StringLen($string) If StringMid($string, $i, StringLen($substring)) = $substring Then $count = $count + 1 Next Return $count EndFunc Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig! Link to comment Share on other sites More sharing options...
SlimShady Posted September 2, 2004 Share Posted September 2, 2004 Yeah. I checked your version carefully. Your version is way better than mine. I don't understand why mine doesn't work like it should... Link to comment Share on other sites More sharing options...
Developers Jos Posted September 2, 2004 Developers Share Posted September 2, 2004 I don't understand why mine doesn't work like it should... <{POST_SNAPBACK}>Its because you replace the SearchString with "" in other words remove it. So in CyberSlugs example the first occurance of "AA" would change "AAA" to "A" .. SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
SlimShady Posted September 2, 2004 Share Posted September 2, 2004 (edited) SlimShady, MsgBox(4096,"", CountSubs ("aaa", "aa")) Returns 1 when it should, arguably, return 2... ; Count the number of times substring occurs in string Func _StringCount($string, $substring) Local $i, $count = 0 For $i = 1 to StringLen($string) If StringMid($string, $i, StringLen($substring)) = $substring Then $count = $count + 1 Next Return $count EndFunc <{POST_SNAPBACK}>Hey! You're wrong! Why would you count the same substring more than once? My script does the following. Step 1. Check if "aa" exists once in "aaa" Result: true; it matches the first time Step 2. Remove it so there are no duplicate matches. eg. Like you said Result: "aaa" becomes "a" Step 3. Check if "aa" exists in "a" Result: no matches; return 1 Edited September 2, 2004 by SlimShady Link to comment Share on other sites More sharing options...
Developers Jos Posted September 2, 2004 Developers Share Posted September 2, 2004 (edited) Hey! You're wrong! Why would you count the same substring more than once? My script does the following. Step 1. Check if "aa" exists once in "aaa" Result: true; it matches the first time Step 2. Remove it so there are no duplicate matches. eg. Like you said Result: "aaa" becomes "a" Step 3. Check if "aa" exists in "a" Result: no matches; return 1 <{POST_SNAPBACK}>There is no wrong or right here... You could argue 1 occurence like you do or 2 occurences like CyberSlug does: AAA and AAA Either way fine ... Edit: fixed wording Edited September 2, 2004 by JdeB SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
magaf Posted September 2, 2004 Author Share Posted September 2, 2004 thanks to both of you and i think a function like the above should be putted in autoit's next version Link to comment Share on other sites More sharing options...
SlimShady Posted September 2, 2004 Share Posted September 2, 2004 (edited) There is no wrong or right here... You could argue 1 occurence like you do or 2 occurences like CyberSlug does: AAA and AAA Either way fine with me ... <{POST_SNAPBACK}>1. You're right. After I posted that, I was thinking: "No one's wrong" 2. I didn't quote you, so you could left your last sentence out. Edited September 2, 2004 by SlimShady Link to comment Share on other sites More sharing options...
CyberSlug Posted September 2, 2004 Share Posted September 2, 2004 By the way, SlimShady used the case-sensitive == operator while I used the weaker = equality test. a == A returns FALSE a = A returns TRUE That might be another thing to check about or an additional paramter you could give the function.... MsgBox(4096,"", _anotherCount ("aAa", "aa", 0)) Func _anotherCount($string, $substring, $caseSense) Local $instance = 1 While StringInStr($string, $substring, $caseSense, $instance) $instance = $instance + 1 Wend Return $instance-1 EndFunc Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig! Link to comment Share on other sites More sharing options...
Developers Jos Posted September 2, 2004 Developers Share Posted September 2, 2004 2. I didn't quote you, so you could left your last sentence out. <{POST_SNAPBACK}> Fixed the wording... SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
trids Posted September 3, 2004 Share Posted September 3, 2004 Ever the one for a lateral solution ... $sBig = "sadfjkh adgxxxxjkh adgh adfjklxxxxgh fsdjklgsxxxx dfjkh sdfg" $sSmall = "xxxx" MsgbOx (0,"" , _StringCount($sBig, $sSmall)) Func _StringCount($psString, $psTarget) return (StringLen($sBig) - StringLen(StringReplace($sBig, $sSmall, ""))) / StringLen($sSmall) EndFunc HTH 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