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, LValue, PValue )

Formal Syntax:

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

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, and string In2 is inserted at the end of string In1.

Examples

ExampleReturn Value
REPLACE('Abcdef', '*', 1, 1 )STRING#'*bcdef'
REPLACE('Abcdef', '**', 1, 2 )STRING#'A**cdef'
REPLACE('Abcdef', '***', 1, 4 )STRING#'Abc***ef'
REPLACE('Abcdef', '*', 4, 2 )STRING#'A*f'

Standards Compliance

Table 29.8 - IEC 61131-3 2nd Edition.

Table 34.8 - IEC 61131-3 3rd 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 3rd 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 definitions of the terms used in Fernhill SCADA.