Sample code for 30+ languages & platforms
Zig

Get the Last N Lines of a StringBuilder

Returns up to the last N lines of the contents of a Chilkat StringBuilder.

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

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

    var i: i32 = 1;
    while (i < 100) {
        sb.append("This is line number ") catch {};
        sb.appendInt(i) catch {};
        sb.append("\r\n") catch {};
        i = i + 1;
    }

    const b_crlf = true;
    std.debug.print("{s}\n", .{try sb.lastNLines(alloc, 12, b_crlf)});

    // Output is:
    //
    //     This is line number 88
    //     This is line number 89
    //     This is line number 90
    //     This is line number 91
    //     This is line number 92
    //     This is line number 93
    //     This is line number 94
    //     This is line number 95
    //     This is line number 96
    //     This is line number 97
    //     This is line number 98
    //     This is line number 99
    //
}