maqleod Posted September 7, 2007 Share Posted September 7, 2007 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 Link to comment Share on other sites More sharing options...
xcal Posted September 7, 2007 Share Posted September 7, 2007 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 How To Ask Questions The Smart Way Link to comment Share on other sites More sharing options...
maqleod Posted September 7, 2007 Author Share Posted September 7, 2007 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 Link to comment Share on other sites More sharing options...
Xenobiologist Posted September 7, 2007 Share Posted September 7, 2007 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 Link to comment Share on other sites More sharing options...
xcal Posted September 7, 2007 Share Posted September 7, 2007 I don't think your StringOccurrences works properly? It seems to return an extra occurrence. How To Ask Questions The Smart Way Link to comment Share on other sites More sharing options...
Vardebedian Posted September 7, 2007 Share Posted September 7, 2007 anyone know a way to find the number of occurances of a string in another string?I wrote this simple function:CODEconsolewrite(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) EndFuncIt works.Bye! Link to comment Share on other sites More sharing options...
Gianni Posted June 2, 2014 Share Posted June 2, 2014 (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 June 2, 2014 by Chimp Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt.... Link to comment Share on other sites More sharing options...
BrewManNH Posted June 2, 2014 Share Posted June 2, 2014 (edited) 6 1/2 year old thread, not the oldest but pushing it for someone that has been here as long as you. Edited June 2, 2014 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 GudeHow 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 Link to comment Share on other sites More sharing options...
Gianni Posted June 2, 2014 Share Posted June 2, 2014 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" Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt.... Link to comment Share on other sites More sharing options...
JohnComputerGuy Posted June 2, 2014 Share Posted June 2, 2014 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 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