Sign in to follow this  
Followers 0
maqleod

number of occurances of a string in another string

10 posts in this topic

anyone know a way to find the number of occurances of a string in another string?


[u]You can download my projects at:[/u] Pulsar Software

Share this post


Link to post
Share on other sites



I made this a bit ago... might work for you.

;  _GetCharPos(string, what to search for)
;  Returns an array of starting positions
;  $array[0] = total returns

#include <array.au3>  ; just for _ArrayDisplay()

$somevar = _GetCharPos('this is some string.  this is the string to find the word is in.', 'is')

_ArrayDisplay($somevar)

Func _GetCharPos($string, $search)
    Local $array[1], $i = 1, $ret
    While 1
        $ret = StringInStr($string, $search, 0, $i)
        If $ret = 0 Then ExitLoop
        ReDim $array[UBound($array) + 1]
        $array[$i] = $ret
        $i += 1
    WEnd
    $array[0] = UBound($array) - 1
    Return $array
EndFunc

Share this post


Link to post
Share on other sites

I made this a bit ago... might work for you.

;  _GetCharPos(string, what to search for)
;  Returns an array of starting positions
;  $array[0] = total returns

#include <array.au3>  ; just for _ArrayDisplay()

$somevar = _GetCharPos('this is some string.  this is the string to find the word is in.', 'is')

_ArrayDisplay($somevar)

Func _GetCharPos($string, $search)
    Local $array[1], $i = 1, $ret
    While 1
        $ret = StringInStr($string, $search, 0, $i)
        If $ret = 0 Then ExitLoop
        ReDim $array[UBound($array) + 1]
        $array[$i] = $ret
        $i += 1
    WEnd
    $array[0] = UBound($array) - 1
    Return $array
EndFunc
it should work, I just gotta figure out the rest of what I'm trying to do to know for sure

[u]You can download my projects at:[/u] Pulsar Software

Share this post


Link to post
Share on other sites

HI,

; RegExp String occurrences

Global $s = "Hello World, I will count letters or Numbers like 1,2,3. And so on. 111!"

MsgBox(0, "", _stringCount($s, "z"))
MsgBox(0, "", _stringCount($s, "1"))
MsgBox(0, "", _stringCount($s, "num", 1)); Case-insensitivity flag.

Func _stringCount($string, $toFind, $case = "(?-i)", $startPosition = 0)
    If $case = 1 Then $case = "(?i)"
    Local $count = StringRegExp($string, $case & $toFind, 3, $startPosition)
    Switch @error
        Case 0
            If IsArray($count) Then Return UBound($count)
        Case 1
            If Not IsArray($count) Then Return 0
        Case 2
            Return -1
    EndSwitch
EndFunc  ;==>_stringCount

Func StringOccurrences($data, $str, $case, $c = 1)
    While StringInStr($data, $str, 0, $c)
        $c += 1
    WEnd
    Return $c
EndFunc  ;==>StringOccurrences

So long,

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

anyone know a way to find the number of occurances of a string in another string?

I wrote this simple function:

CODE

consolewrite(ninstr("Sopra la panca la capra canta sotto la panca la capra crepa!","la") & @crlf)

consolewrite(ninstr("Sopra la panca la capra canta sotto la panca la capra crepa!","panca") & @crlf)

consolewrite(ninstr("Sopra la panca la capra canta sotto la panca la capra crepa!","canta") & @crlf)

consolewrite(ninstr("Sopra la panca la capra canta sotto la panca la capra crepa!","a") & @crlf)

consolewrite(ninstr("That's all folks!","l") & @crlf)

;///////////////////////////////////////////

func nInstr($sWhereSearch, $sWhatSearch)

;///////////////////////////////////////////

local $i = Stringlen($sWhereSearch)

local $j = Stringlen($sWhatSearch)

local $iRet = 0

if $i >= $j Then

$iRet = ( $i - stringlen( StringReplace( $sWhereSearch, $sWhatSearch, "" ) ) ) / $j

EndIf

return($iRet)

EndFunc

It works.

Bye!

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

a little trick can do...

$String = "1 2 3 4 3 1 6 7 5 8 9 4 5 1" ; whole string

$search = "1" ; part to check occurrence

MsgBox(0, "Occurrences", '"' & $search & '" is contained in "' & $String & '" for ' & Occurrences($String, $search) & ' times')

Func Occurrences($String, $search)
    StringReplace($String, $search, $search)
    Return @extended
EndFunc   ;==>Occurrences
Edited by Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

6 1/2 year old thread, not the oldest but pushing it for someone that has been here as long as you. ;)

Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

6 1/2 year old thread, not the oldest but pushing it for someone that has been here as long as you. ;)

 

opsss.... ... I answered without thinking about where I was reading .... :>

I won a good nomination for the ">Zombie thread awards" :idiot:


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

A couple of ways to get there.

StringRegExp

#include<array.au3> $re = StringRegExp(FileRead('yourfile.txt'), 'Total.*?:(d+)', 3) _ArrayDisplay($re)

This code was found here '?do=embed' frameborder='0' data-embedContent>>

 

I have used a ReadFile and StringReplace it returns the number of occurrences of the replace.

$ReadFile = StringReplace($ReadFile,$Word,$SameWord,1,0)  I believe this will return the number of replacements.

Look at the extended return example at the below link.

http://www.autoitscript.com/autoit3/docs/functions/StringReplace.htm

Good Luck

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