方法一:
PublicFunctionFastReplace(SSrch$,SFind$,SRepl$)AsString
DimSrc()AsByte,Dst()AsByte,R()AsByte,F()AsByte
DimLenF&,LenR&,LenDst&,i&,j&,OutPos&
ConstChunkSize&=4096
IfSSrch=””OrSFind=””ThenExitFunction
Src=SSrch:F=SFind:R=SRepl
LenF=UBound(F):LenR=UBound(R)
LenDst=ChunkSize:ReDimDst(0ToLenDst-1)
Fori=0ToUBound(Src)Step2
Forj=0ToLenFStep2
IfSrc(i j)<>F(j)ThenExitFor
Nextj
Ifj>LenFThenFound
Forj=0ToLenRStep2
IfOutPos>=LenDstThen
LenDst=LenDst ChunkSize
ReDimPreserveDst(0ToLenDst)
EndIf
Dst(OutPos)=R(j):OutPos=OutPos 2
Nextj
i=i LenF-1
Else
IfOutPos>=LenDstThen
LenDst=LenDst ChunkSize
ReDimPreserveDst(0ToLenDst)
EndIf
Dst(OutPos)=Src(i):OutPos=OutPos 2
EndIf
Nexti
ReDimPreserveDst(0ToOutPos-2):SSrch=Dst
FastReplace=SSrch$
EndFunction
方法二:
SearchLineisinput,SearchForiswhattosearchfor,ReplaceWithisthereplacement
FunctionsReplace(SearchLineAsString,SearchForAsString,ReplaceWithAsString)
DimvSearchLineAsString,foundAsInteger
found=InStr(SearchLine,SearchFor):vSearchLine=SearchLine
Iffound<>0Then
vSearchLine=””
Iffound>1ThenvSearchLine=Left(SearchLine,found-1)
vSearchLine=vSearchLine ReplaceWith
Iffound Len(SearchFor)-1<Len(SearchLine)Then_
vSearchLine=vSearchLine Right$(SearchLine,Len(SearchLine)-found-Len(SearchFor) 1)
EndIf
sReplace=vSearchLine
EndFunction->