REPLACE - Replaces part of one string with another string

Introduction

Deletes characters from a string, then inserts another string at the position of the deleted characters.

Informal Syntax:

REPLACE( In1, In2, L, P )

Formal Syntax:

REPLACE( In1:=expression, In2:=expression, L:=expression, P:=expression )

Parameters

ParameterTypeDescription
In1ANY_STRINGThe target string.
In2ANY_CHARSThe string to insert.
LANY_INTThe number of characters to delete before the new string is inserted.
PANY_INTThe replace position (1-based).

The function is overloaded and accepts a range of types for the In1 and In2 parameters:

Return value

The return value is the result of deleting L characters at position P from string In1, followed by inserting string In2 at position P.

The type of the return value is the same as the type of the In1 parameter.

Position P is 1-based, where 1 represents the first character in the string, 2 represents the second character in the string, and so on.

If position P is zero or negative, position P is rounded up to 1 (the first character in the string). The function REPLACE deletes L characters from string In1 at position P, then inserts string In2 also at position P.

If position P is greater than the length of the string, no characters are deleted. In this case string In2 is inserted at the end of string In1.

Examples

ExampleReturn Value
REPLACE('Account', '*', 1, 2 )STRING#'A*count'
REPLACE('Modbus', 'icon', 3, 4 )STRING#'Modicon'
REPLACE('ASCII', '*', 3, 2 )STRING#'A*I'

Standards Compliance

Table 29.8 - IEC 61131-3 Second Edition.

Table 34.8 - IEC 61131-3 Third Edition.

Note: The function allows negative values for the P parameter. The IEC 61131-3 standard states this should be an error.

Note: The function allows the P parameter to specify a location outside the input string. The IEC 61131-3 standard states this should be an error.

Note: The function allows different string types as parameters. The IEC 61131-3 Third Edition standard states this should be an error.

Further Information

String Functions

To learn about other String Functions.

Common Elements

To learn about other IEC 61131-3 Common Elements.

Glossary

For the meaning of terms used in Fernhill SCADA.