When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
Published: Tuesday, September 29, 1998

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.


- continued -

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

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

Oh no, this string has stuff like this

Da' Code

'Written by: Toby Gramm
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)
	SearchAndReplace = s
End Function

[Visit Techfools.com]   [Email Toby Gramm]

ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article