Don't click here unless you want to be banned.

LSL Wiki : LibraryStringReplace

HomePage :: PageIndex :: RecentChanges :: RecentlyCommented :: UserSettings :: You are crawl338.us.archive.org

String Replace

Replace a string within a string
simple (yet ugly) code example by Necro Darkes

string strReplace(string source, string pattern, string replace) {
    while (llSubStringIndex(source, pattern) > -1) {
        integer len = llStringLength(pattern);
        integer pos = llSubStringIndex(source, pattern);
        if (llStringLength(source) == len) { source = replace; }
        else if (pos == 0) { source = replace+llGetSubString(source, pos+len, -1); }
        else if (pos == llStringLength(source)-len) { source = llGetSubString(source, 0, pos-1)+replace; }
        else { source = llGetSubString(source, 0, pos-1)+replace+llGetSubString(source, pos+len, -1); }
    }
    return source;
}

// Usage: strReplace("Hello there world.  You are there.", "there", "you");
// Returns: Hello you world.  You are you.

//////////// if (llStringLength(source) == len) makes this possible
// Usage: strReplace("this", "this", "that");
// Returns: that

//////////// if (pos == 0) makes this possible
// Usage: strReplace("No wai!", "No", "Yes");
// Returns: Yes wai!

//////////// if (pos == llStringLength(source)-len) makes this possible
// Usage: strReplace("I will", "will", "won't");
// Returns: I won't

Is anyone able to provide a simpler function to handle this capability?

ScriptLibrary
There is no comment on this page. [Display comments/form]