Liquid Studio Documentation
Data Mapping / Data Mapper Functions / By Category / String / Replace
In This Topic
    In This Topic

    Function Name
    Replace a Search String with a replacement String in an input String
    String Text String to replace part of
    Search String String to replace in input
    Replace String String to replace Search String with in input
    Result Input String with Search String replaced by Replace String
    None -


    The Data Mapper String Replace function returns an input String with a particular search String replaced. If you need to carry out transformations on your input data, you may need to replace characters or substrings as part of that process. In some cases, you may wish to remove a character or substring from an input String - here you can also use the Replace function, by supplying an empty String as the Replace String input. To apply the String Replace transform function, use the following process:

    Create a new Data Mapper file, dragging your XML data source and targets into the editor area. For this example we are using record_source.xsd, an XML Schema Definition inferred from the following source:

    Source XML
    Copy Code

    For our target schema we will be using record_target.xsd, inferred from the following XML:

    Target XML
    Copy Code
    <resource type="Finance">
    <resource type="Human Resources">

    The data models records for an organisation. Notice that the target element content contains a dash character, which appears to act as a separator within the data item, while the target XML contains an underscore. Let's assume that we need to replace the dash character with the underscore character for our target application.

    Here is the Data Mapper with Source and Target imported:

    Data Mapper Source and Target

    Drag the String Replace function from the Component Palette into the mapping area:

    String Replace Component

    String Replace Transform Added

    The String Replace function takes three inputs and gives a String output. The first input should be connected to the input String. The Search String input should be connected to a String defining the character or substring you wish to replace. The Replace String input should be connected to a String defining the characters or substring you want to replace the Search String with. The output should be connected to the target item, which may be in the XML Writer or another function.

    Connect the "file_no" output in the XML Reader to the String input in the Replace component and the Replace Result output to the Element Value in the XML Writer:

    Replace to Reader and Writer

    Now we need two Strings, one representing the dash character and another representing the underscore. Drag two Constant components onto the Mapper and right-click each one in turn, choosing Show Properties. In the first, select String from the Data Type drop-down list and enter a single dash character in the Value field. So that we can see at a glance which String Constant is which, let's alter the Name field to "search".

    Search Constant Properties

    Now set the Properties of the Replace String Constant, entering "replace" as the Name, String as the Data Type and an underscore character as the Value.

    Replace Constant Properties

    Now the purpose of the Constants is clear when you see them in the Mapper.

    String Replace Constants Added

    Connect the search Constant to the Search String input in the Replace function and the replace Constant to the Replace String input.

    String Replace Constants Connected

    Finally, complete the remaining connections between Reader and Writer:

    Input and Output Connections Mapped

    We can now execute the transform by pressing Shift-F5 or the Execute button (Execute). The transform is applied and the file we selected as output opens in the editor:

    Output XML
    Copy Code
     <resource type="Administration">nmjhgfd_654</resource>
     <resource type="Marketing">gtby_7840</resource>

    The file reference value has been altered to include an underscore in place of the dash character as part of the mapping process. You can also use the Replace function with substrings containing multiple characters as either Search or Replace String input.

    This is a scalar function so the standard rules apply if multiple values are presented to one or more inputs.

    See Also