Jump to content

StringInStr occurance


Nova
 Share

Recommended Posts

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

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 ?

Link to comment
Share on other sites

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

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

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

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

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 ?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

<{POST_SNAPBACK}>

Haha, I didn't think of that. Nice idea.

Edit: plus you could easily make it case sensative

Edited by killaz219
Link to comment
Share on other sites

:D

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

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

<{POST_SNAPBACK}>

Link to comment
Share on other sites

The only advantage mine has over yours is that it can record what character the word started at  :P

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
EndFunc

What 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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Had to out do you by 1 :idiot:

Really just becasue I love making gui's :">

Anyways check it out !

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

In a string "aaaaaaaa", how many times does "aa" occur?

4 times? or 7 times?

Dammit your right my script says 7 not 4

Edit: Ok so I have to use string replace ! :idiot: , I only tested it with full words like ......heres a few examples

novas

script

dosent

work

Edited by Nova
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...