|
Post by Nicky Peter Hollyoake on May 5, 2011 20:05:42 GMT -5
Would anyone create me a .DLL with the function "FindAndReplace" having exactly these parameter's & working exactly this way. Sub FindAndReplace(&Word$, Find$, Replace$, IGNORE, char$) dim w$, l$ dim n, i
for i = 0 to len(word$) - 1 l$ = mid$(word$, i + 1, 1) w$ = w$ + l$
'check if we should replace if w$ = Find$ then Word$ = left$(Word$, (i + 1) - Len(w$)) + Replace$ + Right$(Word$, len(Word$) - len(Left$(Word$, i + 1))) endif 'restart to checks if IGNORE then for n = 0 to len(char$) - 1 if l$ = mid$(char$, n + 1, 1) then w$ = "" n = len(char$) - 1 endif next endif next return endsub '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim Word$ = "Something Nicky Hollyoake"
Printr Word$ FindAndReplace(Word$, "Something", "Hello World!", false, "")
Printr Word$ FindAndReplace(Word$, "ollyoake", "ates you.", true, "H")
Print Word$ Thank you. - Nicky
|
|
|
Post by DJLinux on May 6, 2011 11:26:45 GMT -5
["quote"]having exactly these parameter's & working exactly this way.["/quote"] I don't follow wrong ways :lol: Sub FindAndReplaceOld(&Word$, Find$, Replace$, Char$) dim w$, l$ dim n, i
for i = 0 to len(word$) - 1 l$ = mid$(word$, i + 1, 1) w$ = w$ + l$
'check if we should replace if w$ = Find$ then Word$ = left$(Word$, (i + 1) - Len(w$)) + Replace$ + Right$(Word$, len(Word$) - len(Left$(Word$, i + 1))) endif 'restart to checks if len(char$) then for n = 0 to len(char$) - 1 if l$ = mid$(char$, n + 1, 1) then w$ = "" n = len(char$) - 1 endif next endif next return endsub
' ########################################################################### ' # return the position of the first FIND$ in WORD$ from position FROM or 0 # ' ########################################################################### function find(Word$,Find$,From) dim LenWord=Len(Word$) dim LenFind=Len(Find$) dim flag if From<1 then From=1:end if if From>LenWord then return 0:end if while (find$<>mid$(word$,From,LenFind)) and (flag=0) From = From + 1 flag = (From>LenWord) wend ' found ? if (flag=0) then return From else return 0 end if end function
Sub FindAndReplace(&Word$, Find$, Replace$) dim i=Find(word$,find$,1) if i<1 then return :end if dim LenWord=len(word$) dim LenFind=len(find$) ' anything on left side ? dim _Left$ if (i>1) then _Left$=left$(word$,i-1) end if ' anthing on right side ? dim _Right$ if ((i+LenFind)<LenWord) then i=i-1 _Right$=right$(word$,LenWord-(i+LenFind)) end if word$=_left$ + replace$ +_right$ endsub
Sub FindAndReplaceAll(&Word$, Find$, Replace$) dim p=1, i dim LenWord,LenFind=len(find$) dim _Left$,_Right$ i=Find(word$,find$,p) while i>0 ' anything on left side ? if (i>1) then _Left$=left$(word$,i-1) else _Left$="" end if LenWord=len(word$) ' anthing on right side ? if ((i+LenFind)<(LenWord+1)) then i=i-1 _Right$=right$(word$,LenWord-(i+LenFind)) i=i+1 else _Right$="" end if word$=_left$ + replace$ +_right$ p=i+1 i=Find(word$,find$,p) wend end sub
Dim Word$ = "!before! Something Nicky Hollyoake" Printr "yours:" Printr Word$ FindAndReplaceOld(Word$, "Something", "Hello World!","") Printr Word$ FindAndReplaceOld(Word$, "ollyoake", "ates you.","H") Printr Word$:printr
Word$ = "!before! Something Nicky Hollyoake" printr "you mean this?" Printr Word$ FindAndReplace(Word$, "Something", "Hello World!") Printr Word$ FindAndReplace(Word$, "ollyoake", "ates you.") Printr Word$:printr
Word$ = "Hello World :lol:" printr "Find_And_Replace_All()" Printr Word$ FindAndReplaceAll(Word$, "l", "[L]") Printr Word$
|
|
|
Post by Supermonkey on May 6, 2011 18:11:29 GMT -5
Basic4gl doesn't have either an instr or replace function already. Is the only solution is the one presented above? This should really be built in
|
|
|
Post by Nicky Peter Hollyoake on May 6, 2011 19:28:09 GMT -5
Haha...I don't think I made my sentence clear enough.
What I meant is I'm quite happy with my sub-routine I've created my only issue is speed.
Basically yesterday I felt boredom hit me so I decidd to write a routine to read all the line's from a ".gb" file and place it in an array and then using "FindAndReplace" to look for functions/constants, and keywords and add in the tag's for the colours on the forum.
So, I tried to make a "Colour basic4gl code forum kit"? Anyways after successfully doing it I realised it's WAY to slow...Mostly because of the "FindAndReplace" as it's reading the line char by char searching for the words.
- Nicky
|
|
|
Post by DJLinux on May 7, 2011 4:26:05 GMT -5
Find(string$, substring$ [,from%]) is now a part of the ToolBox plugin. Take a look at "string_tools.gb" With Find() you can build your own version of any replace function with Basic4GL. The speed comes than from the use of Left$() and Right$() the slow part was your search for a string (char by char).
It make no sence to put your version of Replace(word$,find$,boolean,Char$) in a DLL Basic4GL must copy in this case 3 strings to the plugin the plugin must find your two strings create a new one and replace a part from it.
Again Left$() and Right$() from Basic4GL will do the same job.
Joshy
|
|