String Concatenate (_)
Details
The string Concatenate operator (_) is a binary (two-operand) operator that interprets its operands as strings and returns a string value.
You use Concatenate to combine string literals, numbers, expressions, and variables. It takes the form:
operand_operand
Concatenate produces a result that is a string composed of the right operand appended to the left operand. Concatenate gives its operands no special interpretation. It treats them as string values.
Examples
The following example concatenates two strings:
WRITE "High"_"chair"
returns Highchair.
When concatenating a numeric literal to another numeric literal or to a non-numeric string, InterSystems IRIS first converts each of the numbers to canonical form. The following example concatenates two numeric literals:
WRITE 7.00_+008
returns 78.
The following example concatenates a numeric literal and a numeric string:
WRITE ++7.00_"+007"
returns the string 7+007.
The following example concatenates two strings and the null string:
SET A="ABC"_""_"DEF"
WRITE A
returns "ABCDEF".
The null string has no effect on the length of a string. You can concatenate an infinite number of null strings to a string.
There is a maximum limit to the length of a string; see String Length Limit. Attempting to concatenate strings that would result in a string exceeding this maximum string size results in a <MAXSTRING> error.
An ObjectScript statement involving multiple concatenations is an atomic (all-or-nothing) operation. In the event of a <MAXSTRING> error, the variable being enlarged by concatenation retains its value prior to the concatenation. For example, if bigstr is a string of length 2,000,000, attempting the concatenation SET bigstr=bigstr_"abc"_bigstr would result in a <MAXSTRING> error. The length of bigstr remains 2,000,000.
Concatenating Encoded Strings
Some ObjectScript strings contain internal encoding that can limit whether these strings can be concatenated:
-
A bit string cannot be concatenated, either with another bit string, with a string that is not a bit string, or with the empty string (""). Attempting to do so results in an <INVALID BIT STRING> error when accessing the resulting string.
-
A List structure string can be concatenated with another List structure string, or with the empty string (""). It cannot be concatenated with a non-List string. Attempting to do so results in an <LIST> error when accessing the resulting string.
-
A JSON string cannot be concatenated, either with another JSON string, with a string that is not a JSON string, or with the empty string (""). Attempting to do so results in an <INVALID OREF> error when accessing the resulting string.