segunda-feira, 26 de julho de 2010

ReplaceSubstring em Ls

Function ReplaceSubstring( strSource As String, strSearch As String, strReplace As String) As String

If (strSearch = strReplace) Then
ReplaceSubstring = strSource
Exit Function
End If
Dim intBegin As Integer
intBegin = 1
Dim intPos As Integer
intPos = Instr( intBegin, strSource, strSearch )
While intPos > 0
strSource = Left$( strSource, Instr( intBegin, strSource, strSearch) - 1) + strReplace + Right$( strSource, Len( strSource) - Instr( intBegin, strSource, strSearch ) - Len( strSearch) + 1 )
intBegin = Instr( intBegin + Len( strReplace ) + 1, strSource, strSearch )
If intBegin > 0 Then
intPos = Instr( intBegin, strSource, strSearch )
Else
intPos = 0
End If
Wend
ReplaceSubstring = strSource
End Function

//////////////////////////////////////////////

Function ReplaceSubstring(text As String, tag As String, tval As String) As String
' Simple Search and Replace function with 3 arguments (if "tag" is not found, "text" will be returned unmodified)
' text : the text to search in
' tag : the existing text or string we are looking for in "text"
' tval : the value that will replace tag

While Instr(1, text, tag, 1) > 0
If Instr(1, text, tag, 1) = 1 Then
text$ = tval$ & Mid(text$, Instr(text$, tag$)+Len(tag$))
Else
text$ = Mid(text$, 1, Instr(text$, tag$)-1) & tval$ & Mid(text$, Instr(text$, tag$)+Len(tag$))
End If
Wend

ReplaceSubstring=text$

End Function

Nenhum comentário:

Postar um comentário