LeftOfAnyString
Short summary
This function edits an AnyString, so only the count of length leftmost characters of the string remain.
If length is greater than the length of the string, the string is unchanged.
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) := "Test";
lengthOfMyString :UDINT := 4;
------------------
LeftOfAnyString(
stringAdress := ADR(myString),
length := 3,
newStringLength => lengthOfMyString
); // myString will be "Tes" with length 3
- Return type:
UDINT
Parameters
| Name | Type | Comment | Kind |
|---|---|---|---|
| stringToHandle | ANY_STRING | string to get the left chars of | 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 LeftOfAnyString: UDINT
VAR_INPUT
(* string to get the left chars of *)
stringToHandle :ANY_STRING;
(* 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:
LeftOfUtf8String(
stringAddress := stringToHandle.pValue,
length := length,
normalizeString := normalizeString,
newStringLength => newStringLength
);
LeftOfAnyString := newStringLength;
__SYSTEM.TYPE_CLASS.TYPE_WSTRING:
LeftOfUtf16String(
stringAddress := stringToHandle.pValue,
length := length,
normalizeString := normalizeString,
newStringLength => newStringLength
);
LeftOfAnyString := newStringLength;
ELSE
; // do nothing
END_CASE