next up previous contents index
Next: Arrays Up: Data Types Previous: Scalars   Contents   Index


Strings

String variables do not need to be declared, and are type assigned when an assignment is first made. Double quote marks are used to delimit literal strings, and are strictly necessary if the string contains spaces or other non-alphanumeric characters.

Whenever a string is defined as a literal in a script or from the Monitor panel in the Script Debugger, it is filtered through a function which converts the following escape codes into the actual character value. The escape codes recognized, from ANSI C Standard X3J11, are

\ a bell
\ b backspace
\ f form-feed
\ n new-line
\ r carriage return
\ t tab
\ v vertical tab
\ ' single quote
\ " double quote
\\ backslash

In addition, forms like `` \ num'' are interpreted as an 8-bit character with ASCII value the 1, 2, or 3-digit octal number num.

When a subscript is applied to a string, the index applies to the string with escapes substituted, e.g., `` \ n'' counts as one character. When a string is printed to the Monitor panel, the reverse filtering is performed.

A special case is the null string, which can be produced by many of the interface functions, usually to signal end-of-input or an error. A null string has no storage. Null strings are not accepted by some functions, so return values from these functions should be tested.

For example:

retstr = Get(blather)
if (retstr == NULL)     # NULL is an alias for 0
#    the string is null
end
if (retstr == "")
#    the string is empty
end

This example above also illustrates the overloading of ``=='' for strings.

The notation can be even simpler:

if (retstr)
#    the string is not null (but may be empty)
else
#    the string is null
end

The [] notation can be used to address individual characters in strings. Also, string1 = string2 + number is accepted, yielding a string pointing at the number'th character of string2. However, it is a fatal error if number is negative, so it is not possible to point backwards into a string. Also, if the number exceeds the text length of the string, a fatal error is generated. A fatal error is an error which will terminate script execution.

Strings are not copied in assignment, so if multiple variables point to the same string, they will all see any modifications to the string. For example:

s1 = "a string"
s2 = s1 + 2
Print(s2)        # prints "string"
s1[4] = ' '
Print(s2)        # prints "st ing"
Print(s1)        # prints "a st ing"

The Strdup function can be used to make an independent copy of an existing string.


next up previous contents index
Next: Arrays Up: Data Types Previous: Scalars   Contents   Index
Stephen R. Whiteley 2024-09-29