EncodeURLComponent

From Xojo Documentation

Method

Encodes the unsafe characters of a URL.

Usage

result = EncodeURLComponent(str)

Part Type Description
result String The encoded string.
str String The string to be encoded.

Notes

A valid URL consists of alphanumerics, special characters and some reserved characters. Use EncodeURLComponent to convert any unsafe characters so they can be properly used in a URL.

The reverse operation is done by DecodeURLComponent.

Sample Code

Here is an example of how an embedded component separator is encoded.

Var s As String
s = EncodeURLComponent("www.bob&jane.com")
// s is "www.bob%26jane.com"

iOS Version

EncodeURLComponent is not available on iOS. Instead you can use a Declare to create your own function:

Function EncodeURLComponent(url As String) As String
Declare Function CFURLCreateStringByAddingPercentEscapes Lib "Foundation" _
(allocator As Ptr, origString As CFStringRef, charactersToLeaveUnescaped As CFStringRef, _
legalURLCharactersToBeEscaped As CFStringRef, encoding As UInt32) As CFStringRef

Var encodedURL As String
encodedURL = CFURLCreateStringByAddingPercentEscapes(Nil, url, Nil, Nil, &h08000100)
Return encodedURL
End Function

See Also

DecodeURLComponent.