Library reference

Remove spaces at both ends

Remove the spaces before and after a name. A space between two words stays in place. These examples use ordinary spaces.

std.mem.trim

Simple example

Trim the single letter.

Start' a '
Resulta
lesson.zig
const std = @import("std");

pub fn main() !void {
    std.debug.print("{s}\n", .{std.mem.trim(u8, " a ", " ")});
}
Output
a
Advertisements

Everyday examples

Remove the extra spaces copied around a ticket number.

Start' B12 '
ResultB12
lesson.zig
const std = @import("std");

pub fn main() !void {
    std.debug.print("{s}\n", .{std.mem.trim(u8, "  B12 ", " ")});
}
Output
B12

Clean up the outside spaces on a shelf label.

Start' Red Apples '
ResultRed Apples
lesson.zig
const std = @import("std");

pub fn main() !void {
    std.debug.print("{s}\n", .{std.mem.trim(u8, " Red Apples  ", " ")});
}
Output
Red Apples

Clean up a name copied from a signup sheet.

Start' Ana '
ResultAna
lesson.zig
const std = @import("std");

pub fn main() !void {
    std.debug.print("{s}\n", .{std.mem.trim(u8, "  Ana ", " ")});
}
Output
Ana
Run the example
Terminal
zig run lesson.zig
Official documentation