Comparing and Searching Bytes
Use std.mem helpers on strings.
Comparing and Searching Bytes is a free Zig Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
You Cannot Use ==
Two slices are different windows, so == compares pointers and lengths, not contents. Comparing text needs a real byte-by-byte check.
std.mem to the Rescue
The standard library module std.mem works on slices of any element type, and that includes your []const u8 strings.
const std = @import("std");
const mem = std.mem;eql Compares Contents
Use mem.eql to test whether two slices hold the same bytes in the same order. It returns a plain bool you can branch on.
mem.eql(u8, "zig", "zig"); // truePass the Element Type
Many std.mem helpers take the element type as their first argument. For strings that argument is always u8, matching []const u8.
Find a Substring
Search for text inside text with indexOf. It returns an optional index: the position of the first match, or null if absent.
mem.indexOf(u8, "hello", "ll"); // 2Handle the null
Because indexOf returns an optional, unwrap it with if or orelse. A null result simply means the needle was not found.
if (mem.indexOf(u8, h, n)) |i| {
// found at i
}Search from the End
Need the last occurrence instead of the first? Reach for lastIndexOf, which scans backward and returns an optional index too.
Prefix and Suffix
Check how a string begins or ends with startsWith and endsWith. Both take u8 and return a clean bool, perfect for parsing.
mem.startsWith(u8, "main.zig", "main");Single-Byte Search
To locate one byte rather than a substring, use indexOfScalar. It is the fast path when you only need a single character.
mem.indexOfScalar(u8, "a,b", ',');Count Occurrences
Use mem.count to tally how many times a needle appears in the text. It returns a usize, never null, even when the count is zero.
mem.count(u8, "aaa", "a"); // 3Case Sensitivity
These helpers are case-sensitive: A and a are different bytes. Normalize case first if you want a case-insensitive match.
Quick Check
You want to know if two strings contain exactly the same text.
Recap
You compared text with mem.eql and searched it using indexOf, startsWith, indexOfScalar, and count, all aware of null and case. 🔍
Frequently asked questions
Is the “Comparing and Searching Bytes” lesson free?
Yes — the full text of “Comparing and Searching Bytes” is free to read here on the web, and the Zig Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Zig Academy course, upgrade to CoddyKit PRO.
What will I learn in “Comparing and Searching Bytes”?
Use std.mem helpers on strings. You practise Zig Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Zig Academy?
No prior experience is required. Zig Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Comparing and Searching Bytes” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Zig Academy lesson?
Yes. Every Zig Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Strings Are []const u8
- String Literals and Escapes
- Comparing and Searching Bytes
- Formatting Text with std.fmt