Writing Character Expressions

In this section:

A character expression returns an alphanumeric or text value.

A character expression can consist of the following components, highlighted below:

Concatenating Character Strings

You can write an expression to concatenate several alphanumeric and/or text values into a single character string. The concatenation operator takes one of two forms, as shown in the following table:

Symbol

Represents

Function

|

Weak concatenation.

Preserves trailing spaces.

||

Strong concatenation.

Suppresses trailing spaces.

Evaluating Character Expressions

Any non-character expression that is embedded in a character expression is automatically converted to a character string.

A constant must be enclosed in single quotation marks (') or double quotation marks ("). Whichever delimiter you choose, you must use the same one to begin and end the string. The ability to use either single quotation marks (') or double quotation marks (") provides the added flexibility of being able to use one kind of quotation mark to enclose the string, and the other kind as data within the string itself.

The backslash (\) is the escape character. You can use it to:

When the backslash is used as an escape character, it is not included in the length of the string.

Example: Using Quotation Marks in a Character Expression

Because you can define a character string using single quotation marks (') or double quotation marks ("), you can use one kind of quotation mark to define the string and the other kind within the string, as in the following expressions:

COMPUTE LastName = "O'HARA";
COMPUTE Msg/A40 = 'This is a "Message"';

Example: Using a Backslash Character (\) in a Character Expression

You can include a backslash (the escape character) within a string as part of the value by preceding it with a second backslash. For example, the following source code

COMPUTE Line/A40 = 'The characters \\\' are interpreted as \'';
.
.
.
TYPE "Escape info:  <Line"

displays:

Escape info:  The characters \' are interpreted as '

When the backslash is used as an escape character, it is not included in the length of the string. For example, a string of five characters and one escape character fits into a five-character variable:

COMPUTE Word/A5 = 'Can\'t'

Example: Specifying a Path in a Character Expression

A path may, depending on the operating system, contain backslashes (\). Because the backslash is the escape character for character expressions, if you specify a path that contains backslashes in an expression, you must precede each of the backslashes with a second backslash. For example:

MyLogo/A50 = "C:\\ibi_img\\AcmeLogo.gif";

Example: Extracting Substrings and Using Strong and Weak Concatenation

The following example shows how to use the SUBSTR function to extract the first initial from a first name, and then use both strong and weak concatenation to produce the last name, followed by a comma (,), followed by the first initial, followed by a period:

First_Init/A1 = SUBSTR (First_Name, 1, 1);
Name/A19 = Last_Name || (', ' | First_Init | '.');

Suppose that First_Name has the value Chris and Last_Name has the value Edwards. The above request evaluates the expressions as follows:

  1. The SUBSTR function extracts the initial C from First_Name.
  2. The expression in parentheses is evaluated. It returns the value
    , C.
  3. Last_Name is concatenated to the string derived in step 2 to produce the following:
    Edwards, C.

    Note that while Last_Name has the format A15, strong concatenation suppresses the trailing spaces.

Variable-Length Character Variables

You can enable a character variable to have a varying length by declaring it either as text (TX) or as alphanumeric with a length of zero (A0). TX and A0 are equivalent.

Specifying a varying length provides several advantages:

Note that the characteristics of variable-length data source fields differ from those of temporary variables. When declaring a data source field, TX is supported for relational data sources, and has a maximum length of 4094 characters. A0 is not supported for data source fields. For information about data source text fields in Maintain Data applications, see the Describing Data With WebFOCUS Language manual.

Example: Padding and Trailing Spaces in Character Variables

Variable-length character variables, unlike those of fixed length, never pad strings with spaces.

For example, if you assign a string of 11 characters to a 15-character fixed-length alphanumeric variable, the variable pads the value with four spaces to make up the difference.

For example, the following source code

COMPUTE Name/A15 = 'Fred Harvey' ;
TYPE "<<Name End of string" ;

displays:

Fred Harvey    End of string

If you assign the same string of 11 characters to a variable-length variable, the variable stores the original value without padding it. For example, the following source code, in which Name is changed to be of variable length (specified by A0)

COMPUTE Name/A0 = 'Fred Harvey' ;
TYPE "<<Name End of string" ;

displays:

Fred HarveyEnd of string

If you assign a string with trailing spaces to a variable (of either fixed or varying length), the variable preserves those spaces. For example, the following source code

COMPUTE Name/A0 = 'Fred Harvey    ' ;
TYPE "<<Name End of string" ;

displays:

Fred Harvey  End of string