Sample code for 30+ languages & platforms
Zig

URL Encoding and Decoding

See more Encryption Examples

Demonstrates URL encoding and decoding.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

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

    const sb = try chilkat.StringBuilder.init();
    defer sb.deinit();
    try sb.append(s);

    // URL encode the string.
    sb.encode("url", "utf-8") catch {};

    // Show the URL encoded string:
    const s_encoded = try sb.getAsString(alloc);
    std.debug.print("{s}\n", .{s_encoded});

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

    // If you prefer "+" instead of "%20" for SPACE chars:
    _ = sb.replace("%20", "+");
    std.debug.print("{s}\n", .{try sb.getAsString(alloc)});

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

    // To decode:
    sb.decode("url", "utf-8") catch {};
    std.debug.print("{s}\n", .{try sb.getAsString(alloc)});

    // Result is: Why a > b?
}