Skip to main content

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

NameTypeCommentKind
stringToHandleANY_STRINGstring to get the substring ofinput
startUDINTposition of first character of the substring (first char in string has position 1)input
lengthUDINTcount of characters in in new substringinput
normalizeStringBOOLnormalize the string before creating subtringinput
newStringLengthUDINTnew length of the string in bytesoutput

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