Jump to content

count a string's appearance


magaf
 Share

Recommended Posts

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

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

  • Developers

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

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

  • Developers

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

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

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

  • Developers

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

<{POST_SNAPBACK}>

:ph34r: 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

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

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