To read the article online, visit http://www.4GuysFromRolla.com/webtech/092998-1.shtml

VBScript Search and Replace Code


This article was written by Toby Gramm, webmaster of Techfools.com. In the article is a function which will search through a text string, replacing one substring with another.

SearchAndReplace(expression, find, replacewith)


Ok, sure you could use the VBScript Replace() Function, but try moving your script into VB.
Darn!, No replace funciton in VB. So I use this, and it works great in Access too!
Note: VB6 has the replace() function

Example:
Dim s
s = "Oh no, this string has stuff I don't want, like this"
Response.Write SeachAndReplace(s, "I don't want, ","")

Result:
Oh no, this string has stuff like this

Da' Code


'Written by: Toby Gramm
'040897
Function SearchAndReplace(s, Str1, Str2)
	Dim pos
	s = s & "" 'Fix Null
	pos = InStr(s, Str1)
	While pos > 0
		s = Mid(s, 1, pos - 1) & Str2 & Mid(s, pos + Len(Str1))
	    pos = InStr(pos, s, Str1)
	Wend
	SearchAndReplace = s
End Function


[Visit Techfools.com]   [Email Toby Gramm]


Article Information
Article Title: VBScript Search and Replace Code
Article Author: Scott Mitchell
Published Date: Tuesday, September 29, 1998
Article URL: http://www.4GuysFromRolla.com/webtech/092998-1.shtml


Copyright 2017 QuinStreet Inc. All Rights Reserved.
Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers