STRING: Difference between revisions
Jump to navigation
Jump to search
Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link
m (Protected "STRING" ([Edit=Allow only autoconfirmed users] (indefinite) [Move=Allow only autoconfirmed users] (indefinite))) |
No edit summary |
||
(5 intermediate revisions by the same user not shown) | |||
Line 9: | Line 9: | ||
* Literal strings are defined by quotation marks on each end. The quotes will not [[PRINT]] to the screen. | * 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. | * 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. | * Semicolons and commas outside of the string can be used to combine strings in a [[PRINT]] statement only. | ||
* [[LEN]] determines the number of bytes and number of string characters that are in a particular string. | * [[LEN]] determines the number of bytes and 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. | * Literal string ends are designated by quotation marks such as: "text". Use [[CHR$]](34) to add quotes to string values. | ||
Line 37: | Line 37: | ||
* Quotation marks cannot be added. Use [[CHR$]](34) as quotes are used to define the ends of strings. | * 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. | * 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 | * 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! | * 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. | ''Example 1:'' Using a string type suffix with a fixed length byte size in QB64 only. The number designates the fixed string length. | ||
{{CodeStart}} | {{CodeStart}} | ||
var$5 = "1234567" | var$5 = "1234567" | ||
PRINT var$5 | PRINT var$5 | ||
{{CodeEnd}} | {{CodeEnd}} | ||
{{OutputStart}}12345{{OutputEnd}} | {{OutputStart}}12345{{OutputEnd}} | ||
Line 52: | Line 52: | ||
''Example 2:'' Creating a string variable value by adding variable and literal string values. This procedure is called string [[concatenation]]. | ''Example 2:'' Creating a string variable value by adding variable and literal string values. This procedure is called string [[concatenation]]. | ||
{{CodeStart}} | {{CodeStart}} | ||
age% = 10 | age% = 10 | ||
a$ = "I am " + {{Cl|CHR$}}(34) + {{Cl|LTRIM$}}({{Cl|STR$}}(age%)) + {{Cl|CHR$}}(34) + " years old." | a$ = "I am " + {{Cl|CHR$}}(34) + {{Cl|LTRIM$}}({{Cl|STR$}}(age%)) + {{Cl|CHR$}}(34) + " years old." | ||
Line 60: | Line 60: | ||
{{CodeEnd}} | {{CodeEnd}} | ||
{{OutputStart}} | {{OutputStart}} | ||
I am "10" years old. How old are you? | I am "10" years old. How old are you? | ||
{{OutputEnd}} | {{OutputEnd}} | ||
:''Note:'' Since quotation marks are used to denote the ends of literal strings, [[CHR$]](34) must be used to place quotes inside them. | :''Note:'' Since quotation marks are used to denote the ends of literal strings, [[CHR$]](34) must be used to place quotes inside them. | ||
Line 66: | Line 66: | ||
''Example 3:'' How QB64 string type suffixes can fix the length by adding a number of bytes after it. | ''Example 3:'' How QB64 string type suffixes can fix the length by adding a number of bytes after it. | ||
{{CodeStart}} | {{CodeStart}} | ||
strings$5 = "Hello world" | strings$5 = "Hello world" | ||
PRINT strings$5 | PRINT strings$5 | ||
{{CodeEnd}}{{OutputStart}}Hello{{OutputEnd}} | {{CodeEnd}}{{OutputStart}}Hello{{OutputEnd}} | ||
''Example 4:'' STRING values can be compared by the [[ASC]] code value according to [[ASCII]]. | ''Example 4:'' STRING values can be compared by the [[ASC (function)|ASC]] code value according to [[ASCII]]. | ||
{{CodeStart}} | {{CodeStart}} | ||
{{Cl|PRINT}} "Enter a letter, number or punctuation mark from the keyboard: "; | {{Cl|PRINT}} "Enter a letter, number or punctuation mark from the keyboard: "; | ||
valu$ = {{Cl|INPUT$}}(1) | valu$ = {{Cl|INPUT$}}(1) | ||
Line 88: | Line 88: | ||
{{Cl|CASE IS}} > value2$: {{Cl|PRINT}} "greater than m but not z" 'z is already evaluated | {{Cl|CASE IS}} > value2$: {{Cl|PRINT}} "greater than m but not z" 'z is already evaluated | ||
{{Cl|CASE ELSE}}: {{Cl|PRINT}} "other value" 'key entry below A including all numbers | {{Cl|CASE ELSE}}: {{Cl|PRINT}} "other value" 'key entry below A including all numbers | ||
{{Cl|END SELECT}} | {{Cl|END SELECT}} | ||
{{CodeEnd}} | {{CodeEnd}} | ||
: ''Notes:'' [[STRING]] values using multiple characters will be compared by the [[ASCII]] code values sequentially from left to right. Once the equivalent code value of one string is larger than the other the evaluation stops. This allows string values to be compared and sorted alphabetically using [[Greater Than|>]] or [[Less Than|<]] and to [[SWAP]] values in [[arrays]] irregardless of the string lengths. | : ''Notes:'' [[STRING]] values using multiple characters will be compared by the [[ASCII]] code values sequentially from left to right. Once the equivalent code value of one string is larger than the other the evaluation stops. This allows string values to be compared and sorted alphabetically using [[Greater Than|>]] or [[Less Than|<]] and to [[SWAP]] values in [[arrays]] irregardless of the string lengths. | ||
{{PageSeeAlso}} | |||
* [[DIM]], [[DEFSTR]] | * [[DIM]], [[DEFSTR]] | ||
* [[CHR$]], [[ASC]] | * [[CHR$]], [[ASC (function)]] | ||
* [[LEFT$]], [[RIGHT$]], [[MID$]] | * [[LEFT$]], [[RIGHT$]], [[MID$ (function)]] | ||
* [[LTRIM$]], [[RTRIM$]] | * [[LTRIM$]], [[RTRIM$]] | ||
* [[LCASE$]], [[UCASE$]] | * [[LCASE$]], [[UCASE$]] | ||
* [[STR$]] | * [[STR$]] | ||
* [[HEX$]] | * [[HEX$]] | ||
* [[MKI$]], [[MKL$]], [[MKS$]], [[MKD$]], [[_MK$]] | * [[MKI$]], [[MKL$]], [[MKS$]], [[MKD$]], [[_MK$]] | ||
* [[CVI]], [[CVL]], [[CVS]], [[CVD]], [[_CV]] | * [[CVI]], [[CVL]], [[CVS]], [[CVD]], [[_CV]] | ||
* [[LEN]], [[VAL]] | * [[LEN]], [[VAL]] | ||
* [[ASCII]], [[DRAW]] | * [[ASCII]], [[DRAW]] | ||
* [[PRINT]], [[PRINT USING]], [[WRITE]] | * [[PRINT]], [[PRINT USING]], [[WRITE]] |
Latest revision as of 00:45, 26 February 2023
STRING variables or literal values are one byte per character length text or ASCII characters.
Syntax
- 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 and 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$.
- STRING values are compared according to the ASCII code values from left to right until one string code value exceeds the other.
- 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 |
Example 4: STRING values can be compared by the ASC code value according to ASCII.
PRINT "Enter a letter, number or punctuation mark from the keyboard: "; valu$ = INPUT$(1) PRINT value$ value1$ = "A" value2$ = "m" value3$ = "z" SELECT CASE value$ CASE value1$: PRINT "A only" CASE value1$ TO value2$: PRINT "B to m" 'A is already evaluated CASE value1$, value2$, value3$: PRINT "z only" 'A and m are already evaluated CASE IS > value2$: PRINT "greater than m but not z" 'z is already evaluated CASE ELSE: PRINT "other value" 'key entry below A including all numbers END SELECT |
- Notes: STRING values using multiple characters will be compared by the ASCII code values sequentially from left to right. Once the equivalent code value of one string is larger than the other the evaluation stops. This allows string values to be compared and sorted alphabetically using > or < and to SWAP values in arrays irregardless of the string lengths.
See also
- DIM, DEFSTR
- CHR$, ASC (function)
- LEFT$, RIGHT$, MID$ (function)
- LTRIM$, RTRIM$
- LCASE$, UCASE$
- STR$
- HEX$
- MKI$, MKL$, MKS$, MKD$, _MK$
- CVI, CVL, CVS, CVD, _CV
- LEN, VAL
- ASCII, DRAW
- PRINT, PRINT USING, WRITE