Name

Escape Function

Syntax

Escape (string)
string
string

Use: Optional

Data Type: String

The String to be encoded.

Return Value

An encoded Variant of Type string.

Description

Returns an encoded version of string.

Rules at a Glance

  • All Unicode characters 255 and below are converted to % xx format except for A-Z, a-z, 0-9, and _*+-./@. For example, a space is replaced by %20.

Programming Tips and Gotchas

  • The Escape function is not documented in the VBScript documentation.

  • The function corresponds to the JScript escape method.

  • You can use the Escape function to encode an HTML document so that a web browser displays the HTML source rather than interprets it. Alternatively, you can use the HTMLEncode method of the ASP Server object to achieve a similar (and more readable) result.

  • You can use the Escape function to encode an HTTP query string before returning it to a web server.

  • If string contains no spaces, punctuation characters, accented characters, or non-ASCII characters, the Escape function simply returns string unchanged.

Example

The following is a very simple routine that allows you to experiment with encoding character strings:

Option Explicit

Dim sIn, sOut
Do While True
   sIn = InputBox("Enter a string:", "UnescapedString", "")
   If sIn = " Then Exit Do

   sOut = Escape(sIn)

   msgbox "In: " & sIn & vbcrlf & _
Loop

For example, the string:

This is a level-1 head: <H1>Hello!</H1>

returns the string:

This%20is%20a%20level-1%20head%3A%20%3CH1%3EHello%21%3C/H1%3E

VB/VBA Differences

This function is not supported in ...

Get VBScript in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.