Skip to content

StrReplace

StrReplace

Description

Replace occurrences of a Str in string with another Str.

Parameters

Name Direction Description
s in,out Str to modify.
match in Str to match.
replacement in Str to replace with.
count in Maximum number of replacements. -1 means replace all occurences.

Success

Modifies s in place.

Failure

No replacement if match not found.

Usage example (Cross-references)

Usage examples (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) {
        WriteFmt("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);
Last updated on