1. string

Creates a new string of specified length with all elements initialized to zero.

1.1. Syntax

int string → string

1.2. Stack Effects

Table 1. Before
Level Object

0

int (non-negative integer)

Table 2. After
Level Object

0

string (new string of length int)

1.3. Description

string creates a string object of length int, with each element initialized to the integer 0 (null character). The string is allocated in local or global VM according to the current VM allocation mode.

The int operand must be: * A non-negative integer * Not greater than the maximum allowable string length (implementation-dependent)

1.4. PostScript Level

Level 1 and later

1.5. Examples

Creating a string
10 string  % Creates string of 10 null characters
Building a string
3 string
dup 0 65 put  % 'A'
dup 1 66 put  % 'B'
dup 2 67 put  % 'C'
% Result: (ABC)
Buffer allocation
/buffer 100 string def  % Allocate 100-char buffer

1.6. Common Use Cases

1.6.1. String Buffer Creation

% Create buffer for cvs
20 string /buf exch def
123 buf cvs  % Convert number to string

1.6.2. File I/O Buffers

/readbuf 256 string def
file readbuf readline

1.6.3. Dynamic String Construction

% Calculate required size
text1 length text2 length add string
% Build composite string
dup 0 text1 putinterval
dup text1 length text2 putinterval

1.7. Common Pitfalls

All Elements Initialized to Zero - The string contains null characters (ASCII 0), not spaces or empty.
5 string  % Not empty, contains 5 null chars
String Literals vs. string Operator - For known text, use string literals (…​). Use string for dynamic buffers.
(Hello)     % Literal string (preferred for constants)
5 string    % Dynamic allocation (for buffers)
Calculate Size Dynamically - Use length to determine buffer sizes.
original length string  % Same size as original

1.8. Error Conditions

Error Condition

[limitcheck]

int exceeds maximum string length

[rangecheck]

int is negative

[stackunderflow]

No operand on stack

[typecheck]

Operand is not an integer

[VMerror]

Insufficient VM to allocate string

1.9. Implementation Notes

  • Most implementations support strings up to 65,535 characters

  • Strings are mutable (can be modified after creation)

  • String elements are integers 0-255 (byte values)

  • Strings allocated in current VM (local or global)

1.10. Performance Considerations

  • Creating strings is very fast

  • Pre-allocate buffers for repeated use

  • Strings are more memory-efficient than arrays for byte data

  • Consider reusing buffers instead of creating many temporary strings

1.11. See Also


Back to top

Copyright © 2025 Ribose. PostScript is a trademark of Adobe. Distributed under the MIT License.