Function: StrReplace
Function: StrReplace

Function: StrReplace

StrReplace ( STRING, SEQUENCE, REPLACEMENT )

Replaces the first occurrence of a sequence of characters in a string with the replacement string.

Parameters

Parameter Type Description
STRING VALUE(STRING) The string to search in.
SEQUENCE VALUE(STRING) The character sequence to search for.
REPLACEMENT VALUE(STRING) The string to replace the set of found characters with.

Returns

VALUE(STRING) The resultant string from replacing the found sequence of characters with the replacement.

What It Does

This function searches the first string for a sequence of characters and, if found, replaces the sequence with the replacement string, and returns the result. All provided values are interpreted as strings before the check. The check is case-sensitive. Only the first occurrence of the sequence in the first string is replaced - the search starts from the beginning of the string.

If the sequence to search for is not found, the original string is returned.


	a = strReplace("tomato", "to", "__");                    // a = "__mato"
	a = strReplace("Apples to Apples", "Apples", "Grapes");  // a = "Grapes to Apples"
	a = strReplace(12345, 12, "__");                         // a = "__345"
	a = strReplace(100.003, "00", "__");                     // a = "1__.003"
	a = strReplace("ABCDEFG", "a", "_");                     // a = "ABCDEFG"

Example

String Replace Examples


world
{
	start()
	{
		// Replace
		textln(strReplace("Apples and Pears", "Apples", "Oranges"));
		textln(strReplace("Apples and Apples", "Apples", "Oranges"));
		textln(strReplace("Kiwis and Lemons", "Grapefruits", "Peaches"));
		textln(strReplace(100, 0, "Zero"));
		textln("");

		// Replace Last
		textln(strReplaceLast("Apples and Pears", "Apples", "Oranges"));
		textln(strReplaceLast("Apples and Apples", "Apples", "Oranges"));
		textln(strReplaceLast("Kiwis and Lemons", "Grapefruits", "Peaches"));
		textln(strReplaceLast(100, 0, "Zero"));
		textln("");

		// Replace All
		textln(strReplaceAll("Apples and Pears", "Apples", "Oranges"));
		textln(strReplaceAll("Apples and Apples", "Apples", "Oranges"));
		textln(strReplaceAll("Kiwis and Lemons", "Grapefruits", "Peaches"));
		textln(strReplaceAll(100, 0, "Zero"));

		quit;
	}
}
×

Modal Header