StringReplace(string, string, string)

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
);

Parameters

sSource

The source string to be scanned.

sFind

The search string to look for.

sReplace

The replacement string to swap in.


Description

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.



Remarks

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.


Requirements

#include "x3_inc_string"

Version

1.69

Example

// 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
categories:  String Functions


author: Axe Murderer, editors: Mistress, Kolyana