Sign in to follow this  
Followers 0
magaf

count a string's appearance

13 posts in this topic

how can i know the number of times a string is contained in different places in another string?

Share this post


Link to post
Share on other sites



#2 ·  Posted (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 by magaf

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

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...

Share this post


Link to post
Share on other sites

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" ..


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#7 ·  Posted (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! :ph34r:

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 by SlimShady

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Hey! You're wrong! :ph34r:

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 by JdeB

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

thanks to both of you and i think a function like the above should be putted in autoit's next version

Share this post


Link to post
Share on other sites

#10 ·  Posted (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 by SlimShady

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

2. I didn't quote you, so you could left your last sentence out.

<{POST_SNAPBACK}>

:ph34r: Fixed the wording...

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Ever the one for a lateral solution :ph34r: ...

$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 :(

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  
Followers 0