Code: Select all
Function TALLY(SomeString As String,PartString As String) As Long
Dim As Long LenP=Len(PartString),count
Dim As Long position=Instr(SomeString,PartString)
If position=0 Then Return 0
While position>0
count+=1
position=Instr(position+LenP,SomeString,PartString)
Wend
Return count
End Function
Function findAndReplace(original As String , find As String , replace As String) As String
If Len(find) = 0 Then Return original
Var t = tally(original , find) 'find occurencies of find
Dim As integer found , n , staid , m
Var Lf = Len(find) , Lr = Len(replace) , Lo = Len(original)
Dim As integer x = Len(original) - t * Lf + t * Lr 'length of output string
dim As String res = String(x , 0) 'output string
Do
If original[n] = find[0] Then 'got a possible
For m = 0 To Lf - 1
If original[n + m] <> find[m] Then Goto lbl 'nope
Next m
found = 1 'Bingo
End If
If found Then
For m = 0 To Lr - 1
res[staid] = replace[m] 'load the replacerment
staid += 1
Next m
n += Lf
found = 0
End If
lbl:
res[staid] = original[n]
staid += 1
n += 1
Loop Until n >= Lo
Return res
End Function
#Include "file.bi"
Sub savefile(filename As String,p As String)
Dim As Integer n
n=Freefile
If Open (filename For Binary Access Write As #n)=0 Then
Put #n,,p
Close
Else
Print "Unable to save " + filename
End If
End Sub
Function loadfile(file as string) as String
If FileExists(file)=0 Then Print file;" not found":Sleep:end
var f=freefile
Open file For Binary Access Read As #f
Dim As String text
If Lof(f) > 0 Then
text = String(Lof(f), 0)
Get #f, , text
End If
Close #f
return text
end Function
dim as string n1=loadfile("book.htm")
print tally(n1,"hell")
print tally(n1,"Hell")
n1=findandreplace(n1,"hell","heaven")
n1=findandreplace(n1,"Hell","Heaven")
print "Done"
print tally(n1,"hell")
print tally(n1,"Hell")
savefile("NewBook.htm",n1)
print "OK"
sleep