From Xojo Documentation

Revision as of 20:27, 4 April 2021 by Gperlman (talk | contribs) (iOS Version)

Encodes the unsafe characters of a URL.


result = EncodeURLComponent(str)

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


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