SubstringOfAnyString
Short summary
This function edits an AnyString to be a substring of itself. The substring will start at index start with length length.
start should not be greater than the length of string.
It returns the new size of the string (Utf8: in byte, Utf16: in words).
Attention: All strings are handled as null-terminated byte streams
Example:
myString :WString(10) := "TestTest";
lengthOfMyString :UDINT := 8;
------------------
SubstringOfAnyString(
stringAdress := ADR(myString),
start := 3,
length := 3,
newStringLength => lengthOfMyString
); // myString will be "stT" with length 3
- Return type:
UDINT
Parameters
| Name | Type | Comment | Kind |
|---|---|---|---|
| stringToHandle | ANY_STRING | string to get the substring of | input |
| start | UDINT | position of first character of the substring (first char in string has position 1) | input |
| length | UDINT | count of characters in in new substring | input |
| normalizeString | BOOL | normalize the string before creating subtring | input |
| newStringLength | UDINT | new length of the string in bytes | output |
Code
Declaration
FUNCTION SubstringOfAnyString: UDINT
VAR_INPUT
(* string to get the substring of *)
stringToHandle :ANY_STRING;
(* position of first character of the substring (first char in string has position 1)*)
start :UDINT;
(* count of characters in in new substring *)
length :UDINT;
(* normalize the string before creating subtring *)
normalizeString :BOOL := TRUE;
END_VAR
VAR_OUTPUT
(* new length of the string in bytes *)
newStringLength :UDINT := 0;
END_VAR
Implementation
RETURN((stringToHandle.pValue = 0) OR stringToHandle.diSize <= 0);
CASE stringToHandle.TypeClass OF
__SYSTEM.TYPE_CLASS.TYPE_STRING:
SubstringOfUtf8String(
stringAddress := stringToHandle.pValue,
start := start,
length := length,
normalizeString := normalizeString,
newStringLength => newStringLength
);
SubstringOfAnyString := newStringLength;
__SYSTEM.TYPE_CLASS.TYPE_WSTRING:
SubstringOfUtf16String(
stringAddress := stringToHandle.pValue,
start := start,
length := length,
normalizeString := normalizeString,
newStringLength => newStringLength
);
SubstringOfAnyString := newStringLength;
ELSE
; // do nothing
END_CASE