Description
The replace function replaces all occurrences of a substring needle in a given text haystack with the specified replacement string. If the optional parameter ignoreCase is set to 1, case sensitivity is ignored.
Syntax
replace(haystack, needle, replacement, [ignoreCase])
Arguments
| Argument | Description |
|---|---|
haystack |
The input string in which replacements are made. |
needle |
The substring to be replaced. |
replacement |
The string that will replace occurrences of needle. |
ignoreCase (optional) |
If set to 1, case sensitivity is ignored. Default value is 0. |
Return Types
The function returns the modified string where all occurrences of needle have been replaced with replacement.
Examples
| Description | Example |
|---|---|
| Simple replacement without case sensitivity |
replace("I am a Pony", "Pony", "Unicorn")
Result: "I am a Unicorn" |
| Replacement ignoring case sensitivity |
replace("I am a Pony", "pony", "Unicorn", 1)
Result: "I am a Unicorn" |
| Multiple replacements in a string |
replace("Pony Pony Pony", "Pony", "Unicorn")
Result: "Unicorn Unicorn Unicorn" |
Notes
- Use
ignoreCaseto ensure that case sensitivity does not affect the replacement process. - The function replaces all occurrences of
needle, not just the first one.
Comments
0 comments
Please sign in to leave a comment.