Name

Escape Function

Syntax

Escape (string)
string
result

Use: Optional

Data Subtype: String

The String to be encoded.

Return Value

An encoded Variant of Subtype string.

Description

Returns an encoded version of string

Rules at a Glance

  • All spaces, puncuation characters, accented characters, and non-ASCII charaters are replaced with the character substring % xx, where xx is the hexidecimal number that represents the character. For example, a space is replaced by %20.

  • Unicode characters with a value greater than 255 are replaced with the character substring %u xxxx, where xxxx is the hexidecimal number that represents the character.

Programming Notes

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

  • The function corresponds to the JavaScript 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.

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

  • If string contains no spaces, puncuation 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> ...

Get VBScript in a Nutshell 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.