String Guide
|
|
Strings in Programming
A string is a data type composed of letters, numerals, and special characters such as "!". Strings are called strings because they are a string of characters. Strings are usually used to give the player messages and to represent the names of video game characters, places, and items.
Strings can contain numbers, but they are numbers to be used in messages, not math. For example :
pname = "You are Joe 6502, the C64 robot."
' vs.
someMath = 6502 + 64
Quotation Marks
In most programming languages, you cannot use quotation marks in a string because they are used to enclose the string itself. Having more than two sets of quotation marks, one at the beginning and one at the end, will confuse most compilers (and interpreters as well).
' this is correct
someString = "This is a string."
' this causes an error
someString = "This has" too many "question marks"
In the second part of the example above, the compiler will see "this has" and question marks as two different strings. Because too many doesn't have quotation marks around it, the compiler will have no idea what to do with it.
Empty String
A string with no characters is an empty string. This is the of strings and can be used to check if the string contains nothing, or make a string empty or "blank."
aString = "blah" ' not an empty string
emptyString = "" ' its an empty string
aString = "" ' now this variable holds an empty string.
Substring
A substring is a smaller part of a larger string. For example, both "Zapp" and "It" are substrings of "ZappIt". Substrings are only defined by the programmer and the term usually used in situations where you have to find a smaller part of a string in a larger string.
String Variable
In the early days of programming, there were no strings. Instead, you had to DIM (create) an array to hold each character. This isn't necessary anymore. One variable can hold multiple characters. Since double quotes ( " ) enclose the string, double quotes cannot usually be a part of the string itself. In older BASICs, the dollar sign ( $ ) was required as a part of a variable to denote it as a string variable.
DIM A$
In some ancient basics, string variables were actually arrays called subscripted variables. To create a string that can hold 22 characters, you would use the code below :
DIM A$(22)
In modern BASICs, we no longer have to use the dollar sign. Instead, the data type of the variable is declared in the DIM statement or is either a variant (which means it can hold strings, numbers, etc).
dim a
' or
dim a as string
It is also no longer required to declare the length of the string variable. This is done automatically and dynamically by the programming language.