I have two strings. Assume the following:
const a = "Zig";
const b = "zig";
I want the comparison to be case - insensitive and characters will be any UTF-8 character.
When I run the following code:
const a = "Zig";
const b = "zig";
const is_equal = std.mem.eql(u8, a, b);
std.debug.print("is_equal: {}\n", .{is_equal});
I got:
is_equal: false
I want is_equal
to be true. How do I do that?
If your strings are ASCII, you could have use std.ascii.eqlIgnoreCase
. Since your strings are UTF-8, it is better to normalize the string before making the comparison.
You can refer to Unicode String Operations blog post which has an example on how to do the normalization & comparison.
const Normalizer = @import("ziglyph").Normalizer;
const testing = @import("std").testing;
var allocator = std.testing.allocator;
var norm = try Normalizer.init(allocator);
defer norm.deinit();
const str_1 = "Jos\u{E9}";
const str_2 = "Jos\u{65}\u{301}";
const str_3 = "JOSÉ";
// Normalized, case-sensitive comparison.
try testing.expect(try norm.eqlBy(str_1, str_2, .normalize));
// Normalized, case-insensitive comparison.
try testing.expect(try norm.eqlBy(str_2, str_3, .norm_ignore));
See also: