STRING
From QB64 Wiki
STRING variables or literal values are one byte per character length text or ASCII characters.
- DIM variable AS STRING [* byte_length]
- Byte length is optional in DIM statements, but is required in TYPE definitions as a literal or constant INTEGER value.
- Literal strings are defined by quotation marks on each end. The quotes will not PRINT to the screen.
- Quotation marks cannot be placed inside of literal string values! Use CHR$(34) to display " quotes.
- Semicolons and commas outside of the string can be used to combine strings in a PRINT statement only.
- LEN determines the number of bytes or number of string characters that are in a particular string.
- Literal string ends are designated by quotation marks such as: "text". Use CHR$(34) to add quotes to string values.
- Variable suffix type definition is $ such as: text$.
- NOTE: Many Qbasic keyword variable names CAN be used with a STRING suffix($) ONLY! You CANNOT use them without the suffix, use a numerical suffix or use DIM, REDIM, _DEFINE, BYVAL or TYPE variable AS statements!
- Creating a fixed length STRING variable in Qbasic:
- Variable$ = " " ' 1 space creates a one byte string length in a procedure(not fixed)
- Variable$ = SPACE$(n%) ' defined as a n% length string in a procedure(not fixed)
- DIM variable AS STRING * n% ' fixed string length cannot be changed later
- Variable AS STRING * n% ' fixed string length in a SUB parameter or TYPE definition.
- CONST variables can also be used after the constant value is defined.
- QB64 fixed length string type suffixes
- A number after the string variable name $ suffix denotes the fixed string length: X$2 denotes a 2 byte string.
- String Concatenation (addition)
- Must be used when defining a string variable's literal value!
- Concatenation uses the + addition symbol to add literal or variable parts to a string variable value.
- Quotation marks cannot be added. Use CHR$(34) as quotes are used to define the ends of strings.
- Numerical values added must be converted to strings in string variable definitions. See the STR$ function.
- Concatenation can be used in PRINT statements along with semicolons and commas used by PRINT ONLY.
- Semicolons or commas outside of quotes cannot be used to make a string variable's literal string value!
Example 1: Using a string type suffix with a fixed length byte size in QB64 only. The number designates the fixed string length.
var$5 = "1234567" PRINT var$5
12345
- Note: The suffix must keep the same byte length or it is considered a different string variable with a different value!
Example 2: Creating a string variable value by adding variable and literal string values. This procedure is called string concatenation.
age% = 10 a$ = "I am " + CHR$(34) + LTRIM$(STR$(age%)) + CHR$(34) + " years old." b$ = "How old are you?" question$ = a$ + SPACE$(1) + b$ PRINT question$
I am "10" years old. How old are you?
- Note: Since quotation marks are used to denote the ends of literal strings, CHR$(34) must be used to place quotes inside them.
Example 3: How QB64 string type suffixes can fix the length by adding a number of bytes after it.
strings$5 = "Hello world" PRINT strings$5
Hello
See also:
- DIM, DEFSTR
- CHR$, ASC
- LEFT$, RIGHT$, MID$
- LTRIM$, RTRIM$
- LCASE$, UCASE$
- STR$ (decimal to string value)
- HEX$ (decimal to hexadecimal string value)
- MKI$, MKL$, MKS$, MKD$, _MK$ (numerical to ASCII string)
- CVI, CVL, CVS, CVD, _CV (ASCII string to numerical value)
- VAL (function converts string to numerical value)
- ASCII, DRAW
- PRINT, PRINT USING, WRITE