zig-string icon indicating copy to clipboard operation
zig-string copied to clipboard

split and splitToString

Open mvolkmann opened this issue 2 years ago • 1 comments

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"));
        }
    }

mvolkmann avatar Sep 23 '23 20:09 mvolkmann

I know this is old but there is an example here in the getLines method.

peterc-s avatar Jul 15 '24 20:07 peterc-s