Skip to content

String Functions

Coldwave Script provides a set of IEC 61131-3 string functions for manipulation and analysis of STRING values. All position parameters (P) are 1-based following the IEC convention. If a position is out of range or a length is zero/negative, functions return an empty string (or 0 for FIND).

TIP

String results are limited to 1024 bytes. Inputs exceeding this limit are silently truncated.

LEN(S)

Returns the length of string S in characters.

vb
PROGRAM
    PRINT LEN("Hello")   ' Output: 5
    PRINT LEN("")         ' Output: 0
END PROGRAM

CONCAT(S1, S2, ...)

Concatenates two or more strings. CONCAT is variadic — any number of STRING arguments is accepted.

vb
PROGRAM
    DIM v AS INT = 25
    PRINT CONCAT("Sensor=", TO_STRING(v))
    ' Output: Sensor=25

    PRINT CONCAT("A", "B", "C")
    ' Output: ABC
END PROGRAM

TO_STRING(X)

Converts any numeric, BOOL, or time/date value to its string representation.

Accepted input types: all numeric types (ANY_NUM), BOOL, TIME, DATE, TOD, DT, STRING.

vb
PROGRAM
    PRINT TO_STRING(42)        ' Output: 42
    PRINT TO_STRING(3.14)      ' Output: 3.14
    PRINT TO_STRING(TRUE)      ' Output: TRUE
    PRINT TO_STRING(T#1500ms)  ' Output: T#1500ms
END PROGRAM

LEFT(S, L)

Returns the leftmost L characters of S.

vb
PROGRAM
    PRINT LEFT("Hello World", 5)
    ' Output: Hello
END PROGRAM

RIGHT(S, L)

Returns the rightmost L characters of S.

vb
PROGRAM
    PRINT RIGHT("Hello World", 5)
    ' Output: World
END PROGRAM

MID(S, L, P)

Returns L characters from S starting at position P (1-based).

vb
PROGRAM
    PRINT MID("Hello World", 5, 7)
    ' Output: World
END PROGRAM

FIND(S1, S2)

Returns the 1-based position of the first occurrence of S2 in S1. Returns 0 if S2 is not found in S1.

vb
PROGRAM
    PRINT FIND("Hello World", "World")   ' Output: 7
    PRINT FIND("Hello World", "xyz")     ' Output: 0
END PROGRAM

REPLACE(S1, S2, L, P)

Replaces L characters in S1, starting at position P (1-based), with the string S2. The replacement string S2 can be shorter or longer than L.

vb
PROGRAM
    PRINT REPLACE("Hello World", "Earth", 5, 7)
    ' Output: Hello Earth
END PROGRAM

INSERT(S1, S2, P)

Inserts string S2 into S1 at position P (1-based). No characters are removed.

vb
PROGRAM
    PRINT INSERT("HelloWorld", " ", 6)
    ' Output: Hello World
END PROGRAM

DELETE(S, L, P)

Deletes L characters from S starting at position P (1-based).

vb
PROGRAM
    PRINT DELETE("Hello World", 6, 6)
    ' Output: Hello
END PROGRAM