Sample code for 30+ languages & platforms
Zig

StringBuilder SetNth

Demonstrates the SetNth method.

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();

    // The SetNth method is handy for setting a part of a delimited string.
    // For example:

    const sb = try chilkat.StringBuilder.init();
    defer sb.deinit();
    sb.append("red,blue,\"green,purple\",,yellow") catch {};

    const delimiter_char = ",";
    const except_double_quoted = true;
    const except_escaped = true;

    sb.setNth(2, "magenta", delimiter_char, except_double_quoted, except_escaped) catch {};
    // Prints "red,blue,magenta,,yellow"
    std.debug.print("{s}\n", .{try sb.getAsString(alloc)});

    sb.setNth(3, "orange", delimiter_char, except_double_quoted, except_escaped) catch {};
    // Prints "red,blue,magenta,orange,yellow"
    std.debug.print("{s}\n", .{try sb.getAsString(alloc)});

    // What happens if we start with an empty string?
    sb.clear();

    sb.setNth(2, "apple", delimiter_char, except_double_quoted, except_escaped) catch {};
    // Prints ",,apple"
    std.debug.print("{s}\n", .{try sb.getAsString(alloc)});

    sb.setNth(0, "orange", delimiter_char, except_double_quoted, except_escaped) catch {};
    // Prints "orange,,apple"
    std.debug.print("{s}\n", .{try sb.getAsString(alloc)});

    sb.setNth(1, "banana", delimiter_char, except_double_quoted, except_escaped) catch {};
    // Prints "orange,banana,apple"
    std.debug.print("{s}\n", .{try sb.getAsString(alloc)});
}