forked from QB64-Phoenix-Edition/QB64pe
-
Notifications
You must be signed in to change notification settings - Fork 0
Concatenation
Samuel Gomes edited this page Nov 8, 2022
·
1 revision
Concatenation is a process where literal or variable STRING values are combined using the + operator.
value$ = "Literal text" + string_variable$ + "more text."
- The STRING values added can be literal or string variable values or a string FUNCTION value.
- When combining string values in a variable definition, concatenation MUST be used!
- Literal or variable numerical values cannot be used in string concatenation.
- A numerical value can be changed to a string value using the STR$(number), CHR$(code), HEX$, OCT$, MKI$, MKS$, MKD$, MKL$, _MK$ or VARPTR$ functions.
- PRINT does not require any concatenation, but it can be used instead of using semicolons where strings are combined ONLY.
- String values CANNOT be subtracted from a value! Use LEFT$, RIGHT$ or MID$ to get portions of a string value.
Adding quotation marks to a string value using concatenation. Variables cannot be defined using semicolons!
quote$ = CHR$(34) + "Hello World!" + CHR$(34)
PRINT "Bill Gates never said "; quote$; " when he answered the telephone!"
Bill Gates never said "Hello World!" when he answered the telephone!
Inserting numerical values in a PRINT string with semicolons, PRINT USING and PRINT with concatenation.
name$ = "Billy"
boxes% = 102
sales! = 306.00
template$ = "& sold ### boxes for $$####,.##."
PRINT name$; " sold"; boxes%; "boxes for $"; sales!; "."
PRINT USING template$; name$; boxes%; sales!
PRINT name$ + " sold" + STR$(boxes%) + " boxes for $" + LTRIM$(STR$(sales!)) + "."
Billy sold 102 boxes for $ 306 .
Billy sold 102 boxes for $306.00.
Billy sold 102 boxes for $306.
Explanation: Printed numerical values using semicolons have a space on each side. PRINT USING properly formats the string and displays the cent values when they are zero. STR$ converts the number to a string and excludes the right number space, but leaves the sign space. LTRIM$ eliminates the leading sign space between the string number and the $ dollar sign.