StrReplace

Table of Contents

StrReplace

Description

Replace occurrences of a Str in string with another Str.

Parameters

NameDirectionDescription
sin,outStr to modify.
matchinStr to match.
replacementinStr to replace with.
countinMaximum number of replacements. -1 means replace all occurences.

Success

Modifies s in place.

Failure

No replacement if match not found.

Usage example (Cross-references)

    }
    
    void StrReplace(Str* s, const Str* match, const Str* replacement, size count) {
    ValidateStr(s);
    str_replace(s, match->data, match->length, replacement->data, replacement->length, count);
    // Test string replace functions
    bool test_str_replace(void) {
    printf("Testing StrReplace variants\n");
    
    // Test StrReplaceZstr
    Str find    = StrInitFromZstr("World");
    Str replace = StrInitFromZstr("Universe");
    StrReplace(&s1, &find, &replace, 1);
    result = result && (ZstrCompare(s1.data, "Hello Universe") == 0);

Share :