WEB开发网
开发学院WEB开发综合 把符串中的一子串替换为另一子串 阅读

把符串中的一子串替换为另一子串

 2006-02-27 11:41:35 来源:WEB开发网   
核心提示:方法一:PublicFunctionFastReplace(SSrch$,SFind$,SRepl$)AsStringDimSrc()AsByte,Dst()AsByte,R()AsByte,F()AsByteDimLenF&,LenR&,LenDst&,i&,j&,OutPos&ConstChunkSize&=409
方法一:
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>LenFThen'Found

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

Tags:替换

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接