Library reference

Match the start of a string

Check the letters at the start of the text. The result is true only when those letters match.

std.mem.startsWith

Simple example

Does abc start with a?

Start'abc' · 'a'
Resulttrue
lesson.zig
const std = @import("std");

pub fn main() !void {
    std.debug.print("{any}\n", .{std.mem.startsWith(u8, "abc", "a")});
}
Output
true
Advertisements

Everyday examples

Child tickets start with C-. Is this a child ticket?

Start'C-18' · 'C-'
Resulttrue
lesson.zig
const std = @import("std");

pub fn main() !void {
    std.debug.print("{any}\n", .{std.mem.startsWith(u8, "C-18", "C-")});
}
Output
true

Express parcels start with EX. Should this parcel go in the express tray?

Start'BX42' · 'EX'
Resultfalse
lesson.zig
const std = @import("std");

pub fn main() !void {
    std.debug.print("{any}\n", .{std.mem.startsWith(u8, "BX42", "EX")});
}
Output
false

Is this shelf in the LAB section?

Start'LAB-3' · 'LAB'
Resulttrue
lesson.zig
const std = @import("std");

pub fn main() !void {
    std.debug.print("{any}\n", .{std.mem.startsWith(u8, "LAB-3", "LAB")});
}
Output
true
Run the example
Terminal
zig run lesson.zig
Official documentation