Concatenation: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 43: Line 43:




 
{{PageSeeAlso}}
''See also:''
 
* [[PRINT]], [[PRINT USING]]
* [[PRINT]], [[PRINT USING]]
* [[CHR$]], [[STR$]], [[VARPTR$]]
* [[CHR$]], [[STR$]], [[VARPTR$]]

Revision as of 00:26, 29 January 2023

Concatenation is a process where literal or variable STRING values are combined using the + operator.


Usage: 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.


Example 1: 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!


Example 2: 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.


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link