Spec-Zone.ru › ColdFusion

replace

Replaces occurrences of substring1 in a string with obj, in a specified scope. The search is case-sensitive. Function returns original string with replacements made.

replace(string, substring1, obj [, scope]) → returns string

Member Function Syntax

string.replace(substring1, obj[, scope])

  • CF
  • Lucee
  • BL 1.0.0+
  • See Also: replaceNoCase replaceList reReplace

    Argument Reference

    string string
    Required

    String to search

    substring1 string
    Required

    Substring to find within string

    obj any
    Required

    String to replace substring1 with. As of CF 2016+ you can also pass a callback function in this argument function(transform, position, original).

    scope string
    Default: one

    * one: replace the first occurrence * all: replace all occurrences
    Values:
    • one
    • all

    start numeric
    Default: 1

    CF 2021+ Position to start searching in the string.

    Links more information about replace

    • Regular expressions - practical examples - There is find, replace and match functions with and without case sensitivity.

    Examples
    Sample code invoking the replace function

    Replace uppercase 'U' with lowercase 'u'

    Replace in Script Syntax

    getVal = replace("ColdFUsion", "U", "u");
    writeDump(getVal);

    Expected Result: ColdFusion

    Replace uppercase 'O' with lowercase 'o', but only once

    Something similar in Tag Syntax

    <cfset getVal1 = replace("COldFusiOn", "O", "o", "ONE") />
    <cfdump var = #getVal1# />

    Expected Result: ColdFusiOn

    Example using Callback Function

    In CF 2016+ you can pass in a callback function to the third argument of the replace function

    public function upCase(pattern, position, orig) {
        return uCase(pattern);
    }
    result = replace("A man a plan a canal.","an", upCase , "ALL");
    writeOutput(result);

    Expected Result: A mAN a plAN a cANal.

    Example with start argument (Replace lowercase 'o' with uppercase 'O' from the third position)

    In CF 2021+ you can pass position to start searching in the string

    getRes = replace("Love ColdFusion", "o", "O","ALL","3");
    writeOutput(getRes);

    Expected Result: Love COldFusiOn

    © 2012–present Foundeo, Inc. and the CFDocs contributors.
    Licensed under the MIT License.
    ColdFusion is a trademark of Adobe Systems Incorporated.
    https://cfdocs.org/replace

    Spec-Zone.ru

    Настройки Оффлайн Что нового Помощь О нас
    Spec-Zone .ru
    спецификации, руководства, описания, API