zig-string
zig-string copied to clipboard
split and splitToString
Do you have an example of using the split and splitToString methods to get an array containing all the resulting slices or Strings instead of getting them one at a time and having to manage the index?
Here's an example I created where I have to call the method repeatedly to get the pieces:
// TODO: Why must this be var and not const?
var colors = try String.init_with_contents(allocator, "red,green,blue");
defer colors.deinit();
// Splits into []u8 slices.
if (colors.split(",", 0)) |c1| {
assert(std.mem.eql(u8, c1, "red"));
if (colors.split(",", 4)) |c2| {
assert(std.mem.eql(u8, c2, "green"));
}
}
I know this is old but there is an example here in the getLines method.