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

Sections:
Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
XML Info
Information:
Feedback
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.

SearchAndReplace(expression,

- 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

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]


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