Nova Posted January 8, 2005 Posted January 8, 2005 (edited) Is there a way to find out how many times a string is present when using StringInStr ? For example the following searches a txt file for the word yes $file = FileRead($sltd, FileGetSize($sltd)) $find = StringInStr( $file, "Yes") If $find = 0 Then MsgBox(0, "Word finder", "The word yes was not found in the selected text") Else MsgBox(0, "Word finder", "The word was yes found in the selected text") EndIf This will tell the user if the word yes exists in the text file but it will not tell the user how many times the word is present. Ive tryed setting the 4th option on StringInStr to a negative number as prompted by the help file but I always get 255 as a result. example $find = StringInStr( $file, "Yes" , 0, -1) MsgBox(0, "Word finder", "The word was yes found in the selected text " & $find & " times") Any ideas ? Edited January 8, 2005 by Nova
Nova Posted January 8, 2005 Author Posted January 8, 2005 Ok Ive gotten this far, as far as I can make out the below code should return how many times the word Nova is present in the text file @ D:\Users.txt The text file actually contains the word Nova 5 times but the following always returns 2 times. Dim $count = 0 $file = FileRead("D:\Users.txt", FileGetSize("D:\Users.txt")) Do $strpos = StringInStr( $file, "Nova") $count = $count + 1 $file = StringTrimRight ( $file, $file - $strpos ) Until $strpos = 0 If $count = 0 Then MsgBox(0, "Nova word finder", "The word Nova was not found in the selected text") Else MsgBox(0, "Word finder", "The word Nova was found in the selected text " & $count & " times") EndIf Any ideas ?
killaz219 Posted January 8, 2005 Posted January 8, 2005 (edited) Here I wrote up a small function for you. It will write to a file where the text in the string is located and return how many times it's present. If you don't want it to be case sensetive then remove one "=" from the "==" Func SearchString($string, $text) Local $i, $timespresent For $i = 1 To StringLen($string) Local $check $check = StringMid($string, $i, StringLen($text)) If $check == $text Then $timespresent = $timespresent + 1 FileWrite("test.txt", $i & " ") EndIf Next Return $timespresent EndFunc You could probably edit it and make it so it writes to a array instead of a file, but I'm too lazy to do that right now.. Edited January 8, 2005 by killaz219
Nova Posted January 8, 2005 Author Posted January 8, 2005 Never mind figured it out, I was way off but I just learnt alot about strings.
Nova Posted January 8, 2005 Author Posted January 8, 2005 @ killaz219 just saw your responce now, I figured it out a few mins before I got to read your post but tnx anyway. Heres my working model just for comparason sake. Dim $count = 0 $file = FileRead("D:\Users.txt", FileGetSize("D:\Users.txt")) $search = StringInStr( $file, "Nova") If $search = @error Then MsgBox(0, "Nova word finder", "The word Nova was not found in the selected text") Else Do $search = StringInStr( $file, "Nova") $count = $count + 1 $file = StringTrimLeft ( $file, 4 ) Until $search = 0 MsgBox(0, "Word finder", "The word Nova was found in the selected text " & $count -1 & " times") EndIf
closeupman Posted January 8, 2005 Posted January 8, 2005 I know you already found how to do it, but thought might be useful for others looking. I modified Killz code (I had started but liked his version better) and made it a little clearer. $message="Hi there Hi how are you Hi there Hi how am I Hi" $count=StringFoundXTimes($message,"HI") msgbox(0,"",$count) exit Func StringFoundXTimes($text,$find_text) local $i, $foundXtimes=0 For $i = 1 To StringLen($text) Local $check $check = StringMid($text, $i, StringLen($find_text)) If StringInStr($check,$find_text) Then ;since we found the text we can skip the length of ;it and use the rest of the text ;increse the found counter $i=$i+StringLen($find_text) $foundXtimes = $foundXtimes + 1 EndIf Next Return $foundXtimes EndFunc
killaz219 Posted January 8, 2005 Posted January 8, 2005 (edited) Nova im not so sure about your version, I made a users.txt with this in it Mova Bova NovaNova Nova aNova And it reported that "Nova" was in there 8 times BTW: closeupman, you don't need the "Exit" in your example because there is no more lines for autoit to run. Edited January 8, 2005 by killaz219
Nova Posted January 8, 2005 Author Posted January 8, 2005 Im not so sure about my version either now that ove tested it further. If it searches a text file which looks like this nova nova or a text file which looks like this nova nova It works fine but when it searches a txt file like this nova nova It suddenly says theres 3 novas, why its it counting spaces as nova ?
SvenP Posted January 8, 2005 Posted January 8, 2005 You can make it a lot shorter (and maybe faster to run): $message="Hi there Hi how are you Hi there Hi how am I Hi" $count=StringFoundXTimes($message,"HI") msgbox(0,"",$count) exit Func StringFoundXTimes($text,$find_text) StringReplace($text, $find_text, "") return @error EndFunc Regards, -Sven
killaz219 Posted January 8, 2005 Posted January 8, 2005 (edited) SvenP said: You can make it a lot shorter (and maybe faster to run):$message="Hi there Hi how are you Hi there Hi how am I Hi" $count=StringFoundXTimes($message,"HI") msgbox(0,"",$count) exit Func StringFoundXTimes($text,$find_text) StringReplace($text, $find_text, "") return @error EndFuncRegards,-Sven<{POST_SNAPBACK}>Haha, I didn't think of that. Nice idea.Edit: plus you could easily make it case sensative Edited January 8, 2005 by killaz219
closeupman Posted January 8, 2005 Posted January 8, 2005 U're right. I look up the StringReplace function and it's EXAMPLE right there would've answered the first post with the tweak of "" like u have w/out actually replacing anything.Sometimes We don't see the forest for the trees!Good Job Sven SvenP said: You can make it a lot shorter (and maybe faster to run):$message="Hi there Hi how are you Hi there Hi how am I Hi" $count=StringFoundXTimes($message,"HI") msgbox(0,"",$count) exit Func StringFoundXTimes($text,$find_text) StringReplace($text, $find_text, "") return @error EndFuncRegards,-Sven<{POST_SNAPBACK}>
Nova Posted January 8, 2005 Author Posted January 8, 2005 Wow thats so simple..... I completly over looked Stringreplace Cheers SvenP
killaz219 Posted January 8, 2005 Posted January 8, 2005 The only advantage mine has over yours is that it can record what character the word started at I can't stop laughing at how easy it was.
SvenP Posted January 9, 2005 Posted January 9, 2005 killaz219 said: The only advantage mine has over yours is that it can record what character the word started at I can't stop laughing at how easy it was.<{POST_SNAPBACK}>Yeah, to record the position in a file makes it more complex indeed:$message="Hi there Hi how are you Hi there Hi how am I Hi" msgbox(0,"",stringlen($message)) $count=StringFoundXTimes($message,"HI") msgbox(0,"",$count) exit Func StringFoundXTimes($text,$find_text) $NewPos=1 $Position=StringInStr($text,$find_text) while $Position <> 0 and $NewPos <> 0 FileWrite("test.txt", String($Position) & " ") $NewPos = StringInStr(StringRight($text,StringLen($text)-$Position) ,$find_text) $Position = $Position + $NewPos wend StringReplace($text, $find_text, "") return @error EndFuncWhat a mess..However, yours version didn't work properly; you forgot to convert $i into a string before writing it to the file :-)Regards,-Sven
SlimShady Posted January 9, 2005 Posted January 9, 2005 Here's a version I made a long time ago: Func CountSubs($Text, $Subs) Local $num $num = 1 While StringInStr($Text, $Subs, 0, $num) $num = $num + 1 WEnd Return $num - 1 EndFunc
killaz219 Posted January 10, 2005 Posted January 10, 2005 SvenP said: However, yours version didn't work properly; you forgot to convert $i into a string before writing it to the file :-)Regards,-Sven<{POST_SNAPBACK}>If I converted it to a string, would you know what position it's at?
SvenP Posted January 10, 2005 Posted January 10, 2005 killaz219 said: If I converted it to a string, would you know what position it's at?<{POST_SNAPBACK}>Since you called the file "text.txt", I assumed it would be a TEXT file. So converting the positions into a string would give the following (human readable) output of "test.txt":1 10 25 34 46 If you wanted to have the non-readable values as in your original script, please call the file "text.bin" next time :-) (<- that's ment as a joke)Regards,-Sven
Nova Posted January 10, 2005 Author Posted January 10, 2005 (edited) Had to out do you by 1 Really just becasue I love making gui's :"> Anyways check it out ! expandcollapse popup#NoTrayIcon #include "GUIConstants.au3" Opt("GUIOnEventMode",1) $Main_Gui = GUICreate("Novasoft - Word finder", 600, 600) $Group1_files = GUICtrlCreateGroup ("Files", 10, 145, 580, 100) $Group2_options = GUICtrlCreateGroup ("Search options", 10, 255, 580, 100) $Group3_results = GUICtrlCreateGroup ("Results", 10, 360, 580, 00) $Input1_files = GUICtrlCreateInput ("", 150, 165, 300, 25, $ES_READONLY) $Input2_files = GUICtrlCreateInput ("", 150, 205, 300, 25) GUICtrlSetState ( $Input1_files , $GUI_DISABLE ) GUICtrlSetState ( $Input2_files , $GUI_DISABLE ) $Lable1_files = GUICtrlCreateLabel (" Load (Text .txt file) ",30, 169, 100) $Lable2_files = GUICtrlCreateLabel (" Search for (Text) ",30, 209, 100) $Lable3_results = GUICtrlCreateLabel ("Occurance = 0" ,100, 380, 500) $Lable3_results_font = GUICtrlSetFont ($Lable3_results, 9, 600, -1, "Comic Sans MS") $Edit1_results = GUICtrlCreateEdit ("", 30,405,240,140,$ES_AUTOVSCROLL+$WS_VSCROLL + $WS_HSCROLL) $Edit2_results = GUICtrlCreateEdit ("", 325,405,240,140,$ES_AUTOVSCROLL+$WS_VSCROLL + $WS_HSCROLL) $Button1 = GUICtrlCreateButton ( "Browse", 455, 165, 60, 25) GUICtrlSetOnEvent($Button1, "Browse_Pressed") $Button2 = GUICtrlCreateButton ( "Search", 455, 205, 60, 25) GUICtrlSetOnEvent($Button2, "Search_Pressed") GUICtrlSetState ( $Button2 , $GUI_DISABLE ) GUISetState () GuiSetOnEvent($GUI_EVENT_CLOSE, "quit") Func quit() Exit EndFunc Func Browse_Pressed() $sltd = FileOpenDialog ( "Select file to search", @HomeDrive, "Text files (*.txt)" , 1 + 2) If @error Then Else GUICtrlSetData ( $Input1_files, $sltd) GUICtrlSetState ( $Button2 , $GUI_ENABLE) GUICtrlSetState ( $Input2_files , $GUI_ENABLE ) GUICtrlSetState ( $Input2_files , $GUI_FOCUS ) EndIf EndFunc Func Search_Pressed() GUICtrlSetData($Lable3_results, "Occurance = 0" ) GUICtrlSetData($Edit1_results, "") GUICtrlSetData($Edit2_results, "") sleep(250) $sltd = GUICtrlRead ( $Input1_files ) $string = FileRead($sltd, FileGetSize($sltd)) $text = GUICtrlRead ( $Input2_files ) If @error Then Else $location = StringInStr($string, $text, 0, 1) If $location = 0 Then MsgBox(0, "Search result:", "No such string exists") Else $i = 1 $location = StringInStr($string, $text, 0, $i) GUICtrlSetData($Lable3_results, "Occurance = " & $i ) GUICtrlSetData($Edit1_results, @CRLF, 1) GUICtrlSetData($Edit1_results, " Occurance = " & $i & " Location = " & $location, 1) GUICtrlSetData($Edit2_results, $string, 1) Do $i = $i +1 $location = StringInStr($string, $text, 0, $i) If $location = 0 Then ExitLoop Else Sleep(250) GUICtrlSetData($Lable3_results, "Occurance = " & $i ) GUICtrlSetData($Edit1_results, @CRLF, 1) GUICtrlSetData($Edit1_results, " Occurance = " & $i & " Location = " & $location, 1) EndIf Until $location = 0 EndIf EndIf EndFunc While 1 $msg = GUIGetMsg() If $msg = $GUI_EVENT_CLOSE Then ExitLoop Wend This is something I entend to eventually finish when I get some spare time like for example adding options like - case sensetive - highlight word/words that were searched for in second edit field. - add directory searching, so you can search a whole dir of text files looking for certain key words -ect -ect -ect Edited January 10, 2005 by Nova
layer Posted January 10, 2005 Posted January 10, 2005 Larry said: In a string "aaaaaaaa", how many times does "aa" occur?4 times? or 7 times?StringReplace will return 4Think about it...Lar.<{POST_SNAPBACK}>that looks like 8 to me FootbaG
Nova Posted January 10, 2005 Author Posted January 10, 2005 (edited) Quote In a string "aaaaaaaa", how many times does "aa" occur?4 times? or 7 times?Dammit your right my script says 7 not 4Edit: Ok so I have to use string replace ! , I only tested it with full words like ......heres a few examplesnovasscriptdosentwork Edited January 10, 2005 by Nova
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