Skip to main content

Copy

function Copy(S: String; iFrom, iCount: LongInt): String;

Example

procedure ScriptEvent(var Value: variant);
begin
Value := Copy('ABCDEFGHIJ', 4, 3); // DEF
end;

Usage

Copy returns up to iCount characters from a one-based string position, truncating safely at the source end.

Parameters and result

ItemTypeDescription
SStringSource string expression.
iFromLongIntOne-based starting position. Use 1 for the first character.
iCountLongIntMaximum number of characters to return.
ResultStringRequested substring, possibly shorter or empty at a boundary.

Boundary behaviour in the installed runtime

ConditionResult
iFrom = 1Starts at the first character.
iFrom < 1Normalised to the beginning of the string by the current Velox. Prefer 1 explicitly.
iFrom > Length(S)Empty string.
iCount = 0 or negativeEmpty string.
Range extends past the endTruncated to the available suffix.
Empty SEmpty string.

String positions count code units, not user-perceived Unicode characters. With a wide/Unicode actual value, slicing between the two UTF-16 surrogates of a supplementary character can produce invalid text. The normal public String is ANSI in this Velox scripting build, so multibyte text can also be cut at an encoded-character boundary.

Additional Technical Info

Copy returns a substring containing at most iCount characters, starting at the one-based position iFrom. It does not change the source value.

The example is fictional and source-reviewed only.

PascalScript dispatch

This is modified PascalScript standard-function selector 4. Although the generated declaration says String, the runtime handler examines the actual value type and invokes Delphi Copy for ANSI, WideString or UnicodeString. Other types are rejected rather than converted automatically.

The result is newly constructed string data. It is not a live view into S and changes to a variable later do not update the earlier result.

External references

Created 2026-07-15