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

    Function Name
    Return the index of an input search String within a target String
    String Text String to search within
    Search String Text String to search for
    Start Index Index to search from within target String
    Result Index of the search String within the target String
    None -


    The Data Mapper String IndexOf function returns the index of a given search String within a target String. If you need to carry out transformations on your input data, you may need to calculate the indices of particular items as part of that process. For example, you may wish to retrieve a particular section of a String but may not know in advance what length the resulting String will be. For this reason, the IndexOf function is most likely to be used in conjunction with other mapping functions. To apply the String IndexOf 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 comprises a few numbers. Let's assume that we only want the map the numerical part of the "file_no" element, which appears after a dash character in the source. We will use the IndexOf function to work out what index the dash is at, then use that information to retrieve the portion of the String following it. We have no knowledge of how long each String section will be in advance, or the length of the String as a whole.

    Here is the Data Mapper with Source and Target imported:

    Data Mapper Source and Target

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

    String IndexOf Component

    String IndexOf Transform Added

    The String IndexOf function takes three inputs and gives a numerical output. The String input should be connected to the target String element, with the Search String input connected to the String you want to retrieve the index of. The Start Index needs to be connected to a number representing the index you want to search from. We will use Constants to represent the Search String and Start Index. The output should be connected to the target item, in many cases this will be another function rather than an item in the XML Writer.

    Connect the "file_no" output in the XML Reader to the String input in the IndexOf component:

    Reader to IndexOf

    Now we need a String representing the dash character. Drag a Constant component onto the Mapper and right-click it, choosing Show Properties. Select String from the Data Type drop-down list and enter a single dash character in the Value field - click OK.

    String Constant Properties

    Connect the Constant output to the IndexOf Search String input.

    Constant to IndexOf

    Now we need another Constant to represent the Start Index, so drag one onto the Mapper and right-click to set its properties. Choose an integer Data Type and enter 0 as the Value, so that we search the entire String.

    Int Constant Properties

    Connect the new integer Constant output to the Start Index input in the IndexOf component.

    Int Constant to IndexOf

    Since the numerical part of the input String is at the section after the dash character, we can use the Right function to retrieve it, so drag one onto the Mapper and connect the "file_no" output in the XML Reader to its String input.

    IndexOf to Right

    The Right function needs to know the length of the right String section we wish to retrieve, which we are going to use the IndexOf function to calculate. By subtracting the index of the dash character from the length of the target String, then subtracting 1 from the result, we will arrive at the length of the String section following the dash. Drag a Length function onto the Mapper and connect its input to the target "file_no" output in the XML Reader.

    Reader to Length

    Drag a Subtract function from the Maths section onto the Mapper and connect its first input to the Length output, with its second input connected to the result of the IndexOf function.

    IndexOf to Subtract

    Now we need to subtract 1, so drag another Constant onto the Mapper and right-click to set the Properties. Choose an integer type and enter 1 as the Value.

    Int Constant Properties

    Drag another Subtract function onto the Mapper, connecting its first input to the previous Subtract output, and the second input to the Constant just added.

    String IndexOf Second Subtraction

    Connect the output of this second Subtract function to the Length input of the Right function, connecting the output of the Right function to the Element Value in the XML Writer.

    String IndexOf Right to Writer

    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">654</resource>
     <resource type="Marketing">7840</resource>

    The file reference value has been restricted to the characters following the dash as part of the mapping process. The IndexOf function may not always be used with such complex mapping processes, but it is most likely to be used in conjunction with other functions.

    If the Search String is not found, the IndexOf function will output a value of -1. You can use this value in conjunction with the Comparator functions, for example to determine whether it is less than zero, in which case the Search String is not present.

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

    See Also