1. string
Creates a new string of specified length with all elements initialized to zero.
1.2. Stack Effects
| Level | Object |
|---|---|
0 |
|
| Level | Object |
|---|---|
0 |
|
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.5. Examples
10 string % Creates string of 10 null characters
3 string
dup 0 65 put % 'A'
dup 1 66 put % 'B'
dup 2 67 put % 'C'
% Result: (ABC)
/buffer 100 string def % Allocate 100-char buffer
1.6. Common Use Cases
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 |
|---|---|
[ |
int exceeds maximum string length |
[ |
int is negative |
[ |
No operand on stack |
[ |
Operand is not an integer |
[ |
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
-
length- Get string length -
get- Get character code -
put- Put character code -
getinterval- Get substring -
putinterval- Put substring -
cvs- Convert object to string