Jump to content

Recommended Posts

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
Posted

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
Posted

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!
  • Developers
Posted

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

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
  • Developers
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

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

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
Posted

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!
Posted

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

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...