StrSplit

Table of Contents

StrSplit

Description

Split the given Str object into multiple Str objects stored in a vector of Str objects. Each Str object in returned vector is a new Str object and hence must be deinited after use. Calling VecDeinit() on the returned vector will do that for you automatically for all the objects. This is best used when iterating over a delimited data is not the only goal, but also other modifications like stripping over whitespaces from returned Str objects.

Parameters

NameDirectionDescription
strinStr object to split
keyinZero-terminated char pointer value to split based on

Success

Strs vector of non-zero length

Failure

Strs vector of zero-length

Usage example (Cross-references)

    }
    
    Strs StrSplit(Str* s, const char* key) {
    ValidateStr(s);
    Str file = StrInit();
    if (ReadCompleteFile("Bin/Demangler/CppNameManglingGrammar", &file.data, &file.length, &file.capacity)) {
    Strs lines = StrSplit(&file, "\n");
    
    // Use the fixed VecForeachPtr macro
    // Test string split functions
    bool test_str_split(void) {
    printf("Testing StrSplit and StrSplitToIters\n");
    
    // Test StrSplit
    // Test StrSplit
    Str  s     = StrInitFromZstr("Hello,World,Test");
    Strs split = StrSplit(&s, ",");
    
    bool result = (split.length == 3);

Share :