Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

a better way to do this script - using lots of replace() 1

Status
Not open for further replies.

tester321

Programmer
Mar 13, 2007
150
CA
Hi i was wondering i someone was crafty enough to think of a better way to write this function, thanks:
Code:
Function Enc(str)
		If Isnull(str) Then
			Enc=""
		Else
			Enc=Replace(Replace(Replace(Replace(Replace(Replace(str,"\","\\"),"{","\{"),"}","\}"),"'","\'"),"(","\("),")","\)")
		End if
End Function
 
Function Enc(str)
Dim lLBound
Dim lUBound
Dim lCtr
Dim sAns
dim FindChars(6)
findchars(0)="\"
findchars(1)="{"
findchars(2)="}"
findchars(3)="'"
findchars(4)="("
findchars(5)=")"
lLBound = LBound(FindChars)
lUBound = UBound(FindChars)

sAns = str

For lCtr = lLBound To lUBound
sAns = Replace(sAns, FindChars(lCtr), "\" & FindChars(lCtr))
Next

return sAns
End Function

ReplaceMultiple = sAns
 
Looks like a job for a regular expression:
Code:
DIm teststring : teststring = "this is a test: slash\ bracket{ bracket} singlequote' lparen( rparen) whee"
Response.Write "teststring Unescaped is: " & teststring & "<br/>"
Response.Write "teststring Escaped is: " & EscapeStuff(teststring)

Function EscapeStuff(str)
	Dim regex
	Set regex = New RegExp
	regex.Pattern = "([\\{}'()])"
	regex.Global = True
	EscapeStuff = regex.Replace(str,"\$1")
	Set regex = Nothing
End Function

No loops involved and easily scaled by adding more characters to the pattern.

-T

Best MS KB Ever:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top