String Expressions and Concatenation
A string expression can be any of the following:
-
A string literal. For example,
"David"
. -
A symbol with a string value. For example,
NAME
, where NAME has been equated to "David". -
A variable with a string value. For example,
NAME
, where NAME = "David". -
A substring. A set of characters that make up part of a whole string. For example,
NAME[1,3]
evaluates to "Dav". -
The result of any DataBasic function that returns a string.
-
Any of the above concatenated together. For example,
if NAME = "David", NAME[1,3]:"e"
evaluates to "Dave". String expressions can also be concatenated with arithmetic expressions.
Substring Expression
Substrings are specified by a starting character position (start#) and a substring length (length), separated by a comma and enclosed in square brackets, that is,
[start#,length]
For example, if STRING = "EXAMPLE", the substring expression STRING[3,4]
evaluates to "AMPL", that is, the four-character substring starting
at the third character (letter A).
If a negative number is specified as the starting character position, DataBasic then counts backwards from the end of the string to find the first character of the substring.
For more information on substrings, refer to the topics Substring Extraction and Substring Assignment.
Concatenation
Strings can be concatenated using the colon (:) or CAT operator. Concatenation appends the characters of the second string operand onto the end of the first.
For example, both of the following concatenation expressions evaluate to the same concatenated string value, "An example of concatenation":
"An example of" CAT " concatenation"
"An example of ":" concatenation"
String expressions can also be concatenated with arithmetic expressions. For example,
2*3+4:" green bottles"
evaluates to "10 green bottles"
The arithmetic expression is evaluated first. The result is then treated by the concatenation operator as a string value.
Multiple concatenation operations are performed from left to right with subexpressions in parentheses evaluated first.
Examples
In the following examples:
B = "EXAMPLE" A = "ABC123".
These expressions represent substrings of the string variables A and Z
C=B[1,4] evaluates to EXAM
C=A CAT B evaluates to ABC123EXAMPLE
C=B[-2,2] evaluates to LE
C=A:B[1,1] evaluates to ABC123E
C=A[6,1]+5 evaluates to 8
C=B CAT " ONE" evaluates to EXAMPLE ONE