Name

UniqueString Procedure

Syntax

procedure UniqueString(var Str: string);

Description

The UniqueString procedure ensures that Str has a reference count of one. If Str has no other references, UniqueString leaves it alone; otherwise, UniqueString allocates a new copy of the string and assigns that unique copy to Str.

UniqueString is a real procedure.

Tips and Tricks

  • You do not usually need to call UniqueString. Delphi uses copy-on-write semantics for strings, so if you have multiple references to a string and you modify the string through one reference, Delphi automatically creates a unique string to modify, so the other references continue to refer to the original string.

  • If you cast a string to PChar, Delphi also ensures that the PChar points to a unique instance of the string. If you modify the string through the PChar pointer, the other references to the original string are safe.

See Also

String Keyword

Get Delphi in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.