Concatenation: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
(Created page with "'''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 valu...")
 
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 7: Line 7:
* The [[STRING]] values added can be literal or string variable values or a string [[FUNCTION]] value.
* 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!
* When combining string values in a variable definition, concatenation MUST be used!
* Literal or variable numerical values cannot be used in string concatenation.  
* 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.
* 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 [[semicolon]]s where strings are combined ONLY.
* [[PRINT]] does not require any concatenation, but it can be used instead of using [[semicolon]]s 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.
* String values CANNOT be subtracted from a value! Use [[LEFT$]], [[RIGHT$]] or [[MID$ (function)|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!  
''Example 1:'' Adding quotation marks to a string value using concatenation. Variables cannot be defined using semicolons!
{{CodeStart}}
{{CodeStart}}
quote$ = {{Cl|CHR$}}(34) + "Hello World!" + {{Cl|CHR$}}(34)
quote$ = {{Cl|CHR$}}(34) + "Hello World!" + {{Cl|CHR$}}(34)
Line 25: Line 25:


''Example 2:'' Inserting numerical values in a PRINT string with semicolons, PRINT USING and PRINT with concatenation.
''Example 2:'' Inserting numerical values in a PRINT string with semicolons, PRINT USING and PRINT with concatenation.
{{CodeStart}} '' ''
{{CodeStart}}
name$ = "Billy"
name$ = "Billy"
boxes% = 102
boxes% = 102
Line 33: Line 33:
{{Cl|PRINT}} name$; " sold"; boxes%; "boxes for $"; sales!; "."
{{Cl|PRINT}} name$; " sold"; boxes%; "boxes for $"; sales!; "."
{{Cl|PRINT USING}} template$; name$; boxes%; sales!
{{Cl|PRINT USING}} template$; name$; boxes%; sales!
{{Cl|PRINT}} name$ + " sold" + {{Cl|STR$}}(boxes%) + " boxes for $" + {{Cl|LTRIM$}}({{Cl|STR$}}(sales!)) + "."  
{{Cl|PRINT}} name$ + " sold" + {{Cl|STR$}}(boxes%) + " boxes for $" + {{Cl|LTRIM$}}({{Cl|STR$}}(sales!)) + "."
{{CodeEnd}} '' ''
{{CodeEnd}}
{{OutputStart}}
{{OutputStart}}
Billy sold 102 boxes for $ 306 .
Billy sold 102 boxes for $ 306 .
Line 43: Line 43:




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

Latest revision as of 00:32, 26 February 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