Sample code for 30+ languages & platforms
PureBasic

URL Encoding and Decoding

See more Encryption Examples

Demonstrates URL encoding and decoding.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkStringBuilder.pb"

Procedure ChilkatExample()

    success.i = 0

    ; To URL encoding a string:
    s.s = "Why a > b?"

    sb.i = CkStringBuilder::ckCreate()
    If sb.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkStringBuilder::ckAppend(sb,s)

    ; URL encode the string.
    CkStringBuilder::ckEncode(sb,"url","utf-8")

    ; Show the URL encoded string:
    sEncoded.s = CkStringBuilder::ckGetAsString(sb)
    Debug sEncoded

    ; The result is:  Why%20a%20%3E%20b%3F

    ; If you prefer "+" instead of "%20" for SPACE chars:
    numReplaced.i = CkStringBuilder::ckReplace(sb,"%20","+")
    Debug CkStringBuilder::ckGetAsString(sb)

    ; Output is:   Why+a+%3E+b%3F

    ; To decode:
    CkStringBuilder::ckDecode(sb,"url","utf-8")
    Debug CkStringBuilder::ckGetAsString(sb)

    ; Result is: Why a > b?


    CkStringBuilder::ckDispose(sb)


    ProcedureReturn
EndProcedure