
OpenNTF Code Bin
About This Code
Brief Description:
ReplaceSubstring
Contributor:
Jani Krunniniva
Notes Version:
R4.x, R5.x, R6.x
Last Modified:
03 Dec 2002
OpenNTF Disclaimer
All of the program code and information presented in the OpenNTF.org Code Bin are provided "as-is", and should be used at your own risk. OpenNTF.org make no express or implied warranty about anything in the Code Bin, and OpenNTF.org will not be responsible or liable for any damage caused by the use or misuse of anything from this site. OpenNTF.org makes no guarantees about anything. Please thoroughly test all of the knowledge and code you find here before you attempt to use them in your production environment.
Code / Description
Function ReplaceSubstring (fullstr As String, oldstr As String, newstr As String) As String
lenold = Len (oldstr)
pos = Instr (fullstr, oldstr)
Do While pos > 0 And oldstr <> ""
fullstr = Left (fullstr, pos - 1) & newstr & Mid (fullstr, pos + lenold)
pos = Instr (pos + Len (newstr), fullstr, oldstr)
Loop
ReplaceSubstring = fullstr
End Function
Usage / Example
Example of use:
Sub Click(Source As Button)
Dim strTest As String
strTest = ReplaceSubstring ( "old Test String", "old", "new" )
End Sub
Returns "new Test String"
Comments
Posted by Jani Krunniniva on 10/17/2002 01:20:40 AMContributor was Jani Krunniniva :)
Posted by John Smart on 10/22/2002 04:00:35 PMThanks, and edit tip
Posted by John Smart on 07/03/2003 09:11:06 AMThanks, and edit tip
Thanks Janni. This is what I was looking for. I didn't like the other ReplaceSubstring forumulas
in this db because they went into an infinite loop if the fromstring was contained in the tostring.
Also, to edit your document after it's been posted (to give yourself credit, you need to look at
your document, then replace the "OpenDocument" part of the URL with "EditDocument"
For example, click here:
http://www.openntf.org/Projects/CodeBin/codebin.nsf/e4bf5e5e5c3f821e88256bda006dac0c/49f197271969496
088256c550022c055?EditDocument
Posted by Pierre Koerber on 11/30/2004 08:44:38 AMRe: ReplaceSubstring
This is the single replacesubstring which is working with the belowed sampe :
Original : hello
toReplace : l
target : ll
result : hellllo
Posted by John Smart on 12/15/2004 10:33:36 AMImproved version
Thanks again! I used this yet again but had to make a few small improvements due to situation requirements. Changed to conform with naming standards, all variables declared, and all parameters use Byval (which allows for use within forall loops and changes in sFull aren't passed back within the parameter).
Function ReplaceSubstring (Byval sFull As String, Byval sOld As String, Byval sNew As String) As String
'Thanks to Jani Krunniniva
'http://www.openntf.org/Projects/codebin/codebin.nsf/CodeBySubContributor/49F197271969496088256C550022C055
Dim iLenOld As Integer
Dim iPos As Integer
iLenOld = Len (sOld)
iPos = Instr (sFull, sOld)
Do While iPos > 0 And sOld <> ""
sFull = Left (sFull, iPos - 1) & sNew & Mid (sFull, iPos + iLenOld)
iPos = Instr (iPos + Len (sNew), sFull, sOld)
Loop
ReplaceSubstring = sFull
End Function
Posted by Kee Keekkenen on 03/20/2008 06:53:06 PMsorry, but you code is very slow
this more best production code
Function ReplaceSubstring2(source As String, subString As String, newString As String) As String
try:
On Error Goto catch
ReplaceSubstring2 = Implode(Split(source, subString ), newString )
Exit Function
catch:
Print Err, Error, Erl
ReplaceSubstring2 = source
Exit Function
End Function