StringReplace(string, string, string)

From NWN Lexicon
Jump to: navigation, search

Given a source string, search string, and replacement string, this function returns the result of replacing every occurrence of the search string found in the source string with the replacement string.

string StringReplace(
    string sSource,
    string sFind,
    string sReplace
Red bug icon.png Warning: This function has a known bug and may not work as expected in some situations. See Known Bugs for details.


The source string to be scanned.
The search string to look for.
The replacement string to swap in.


Given a source string, search string, and replacement string, this function searches through the source string looking for occurrences of the search string, and replaces every occurrence found with the replacement string.


If either the source string specified in the sSource parameter is empty (""), or the search string specified in the sFind parameter is not found in the source string, the entire source string is returned.

If the sFind string is blank, the function will repeatedly prepend the replacement string onto the front of the source string until a Too Many Instructions error occurs. Therefore the sFind parameter must always contain a non-empty string.

If the replacement string specified in the bReplace parameter is blank, the function will return the result of stripping all occurrences of the search string from the source string.

Known Bugs

The function does not handle a blank search string. If the search string is blank, the function will cause a TMI to occur.


#include "x3_inc_string"




// The script below takes the contents of a local string variable
// stored on the calling object, replaces all space characters in
// it with underscores, then stuffs the result back into the local
// variable.
#include "x3_inc_string"
void main()
    // Get the local string variable's contents.
    string sValue = GetLocalString(OBJECT_SELF, "SomeVariable");
    // Change all the spaces in it to underscores.
    sValue = StringReplace(sValue, " ", "_");
    // Save the result back in the local string variable.
    SetLocalString(OBJECT_SELF, "SomeVariable", sValue);

See Also

functions:  StringParse | StringRemoveParsed | FindSubString | GetSubString

author: Axe Murderer, editors: Mistress, Kolyana