Check for empty text
An empty string has no characters. A space is still a character.
slice.len
Simple example
Is a string with no characters empty?
Start''
Resulttrue
const std = @import("std");
pub fn main() !void {
std.debug.print("{any}\n", .{"".len == 0});
}
true
Advertisements
Everyday examples
A library card has no name entered. Should the form ask for one?
Start''
Resulttrue
const std = @import("std");
pub fn main() !void {
std.debug.print("{any}\n", .{"".len == 0});
}
true
A passenger typed a space in the seat field. Is the raw field empty?
Start' '
Resultfalse
const std = @import("std");
pub fn main() !void {
std.debug.print("{any}\n", .{" ".len == 0});
}
false
The classroom screen says Ready. Is the message empty?
Start'Ready'
Resultfalse
const std = @import("std");
pub fn main() !void {
std.debug.print("{any}\n", .{"Ready".len == 0});
}
false