Programmer Guide > Working with Text > String Formatting
  

String Formatting
The STRING function has the form:
result = STRING(Expression1, ..., Expressionn )
It converts its parameters to characters, returning the result as a string expression. It is very similar to the PRINT statement, except that its output is placed into a string rather than being output to the screen. As with PRINT, the Format keyword can be used to explicitly specify the desired format. See the discussions of free format and explicitly formatted I/O in "Choosing Free or Fixed (Explicitly Formatted) ASCII I/O" for details on data formatting.
As a simple example, the following statements:
; Produce a string array.
A = STRING(Format='("The values are:", /, (I))', INDGEN(5))
; Show its structure.
INFO, A
; Print the result.
FOR I = 0, 5 DO PRINT, A(I)
produce the following output:
A  STRING = Array(6)
The values are:
0
1
2
3
4
If you do not use a Format statement when converting numbers to strings in PV-WAVE or printing numeric values, the default format codes shown in 32-bit UNIX and Windows, 64-bit UNIX, and 64-bit Windows are applied:
 
Table 8-1: 32-bit UNIX and Windows
Data Type
Format Code
BYTE
%4d
INT
%8d
INT32
%12d
LONG
%12ld
FLOAT
%13.6g
DOUBLE
%16.8g
 
Table 8-2: 64-bit UNIX
Data Type
Format Code
BYTE
%4d
INT
%8d
INT32
%12d
LONG
%21ld
FLOAT
%13.6g
DOUBLE
%16.8g
 
Table 8-3: 64-bit Windows
Data Type
Format Code
BYTE
%4d
INT
%8d
INT32
%12d
LONG
%21lld
FLOAT
%13.6g
DOUBLE
%16.8g
Using STRING with Byte Arguments
There is a close association between a string and a byte array—a string is simply an array of bytes that is treated as a series of ASCII characters. It is therefore convenient to be able to switch between them easily.
When STRING is called with a single argument of type byte and the Format keyword is not used, STRING does not work in its normal fashion. Instead of formatting the byte data and placing it into a string, it returns a string containing the byte values from the original argument. Thus, the result has one less dimension than the original argument. A two-dimensional byte array becomes a vector of strings, a byte vector becomes a scalar string. However, a byte scalar also becomes a string scalar. For example, the statement:
PRINT, STRING([72B, 101B, 108B, 108B, 111B])
produces the output:
Hello
This occurs because the argument to STRING, as produced by the array concatenation operator [ ], is a byte vector. Its first element is 72B which is the ASCII code for “H”, the second is 101B which is an ASCII “e”, and so forth.
As discussed in the section "Explicitly Formatted Input and Output", it is easier to read fixed length string data from binary files into byte variables instead of string variables. It is therefore convenient to read the data into a byte array and use this special behavior of STRING to convert the data into string form.
Another use for this feature builds strings that have unprintable characters in them in a way that doesn’t actually require entering the character directly. This results in programs that are easier to read, and which also avoid file transfer difficulties. (Some forms of file transfer have problems transferring unprintable characters).
For example:
; 9 is the decimal ASCII code for the tab character.
tab = STRING(9B)
; 7 is the decimal ASCII code for the bell character.
bel = STRING(7B)
; Output line containing tab character and ring terminal bell.
PRINT, 'There is a', tab, 'tab here.', bel
Executing these statements gives the output:
There is a     tab here.
and rings the bell.
Applying the STRING function to a byte array containing a null (zero) value will result in the resulting string being truncated at that position. Thus, the statement:
PRINT, STRING([65B, 66B, 0B, 67B])
produces the output:
AB
because the null byte in the third position of the byte array argument terminates the string and hides the last character.
The BYTE function, when called with a single argument of type string, performs the inverse operation to that described here, resulting in a byte array containing the same byte values as its string argument.

Version 2017.0
Copyright © 2017, Rogue Wave Software, Inc. All Rights Reserved.