String variables, assignment statements, and expressions provide the means for storing and manipulating text within GPL. As such, Strings are also the primary means for transferring data in and out of the system via the serial communications ports, the file system, and the Ethernet interface.
String variables store a series of ASCII characters and can be of arbitrary length. However, String operations have been optimized to execute most efficiently on Strings that are 128 characters or less in length.
String constants must be delimited by double quote marks, e.g. "Hello world", and can at most be 128 characters in length. To embed a double quote mark within a String constant, enter two double quote marks in a row, e.g. "Tom said, ""Hello world""".
As with other variables, String arrays are supported and the values of procedure level String variables can be initialized in DIM statements. For example,
Dim name As String = "Charlie"
A number of easy-to-use functions are provided for converting between String values and numerical values, e.g. CStr, CDbl, CInt. Each of these built-in functions was described earlier in the section on Basic Data Types.
As a convenience, GPL automatically converts a String value to a Double whenever a numerical value is expected and a String is encountered instead. For example, the following statements are legal:
Dim a As Double
a = 2.34 + "1.01" ' Legal. a will be equal to 3.35
However, it is generally better practice to utilize the explicit conversion routines rather than relying upon the automatic conversions. The automatic conversions can result in some computations whose results may not be clear.
In most cases, when a String value is required as an input, a String expression can be provided. A String expression can consist of a String variable, constant, function or method or a concatenation of two or more of these String elements.
Two or more String elements can be concatenated together by utilizing the concatenation operator, "&". Also, for compatibility with other Basic compilers, the "+" can alternatively be used to indicate concatenation. However, given the automatic String to numeric conversion features of the language, the use of the "+" can make it less obvious whether a statement is intended to produce a String or a numeric result. Therefore, the use of the "&" concatenation operator is recommended over the "+".
The following is an example of String concatenation.
Dim s1, s2 As String
s1 = "Joe's"
s2 = s1 & " balance: " & CStr(10.2) ' s2 = "Joe's balance: 10.2"
Since String values are often generated by appending additional text on to the end of the value of a String variable, for computational efficiency, the concatenation assignment operator is supported. For example,
s1 &= " more" is equivalent to s1 = s1 & " more"
The advantage of the concatenation assignment operator is that appended text is directed added onto the end of the variable's value. In the standard assignment statement, the value, s1, is copied to an intermediate variable where it is concatenated with the appended String value, " more". The resulting value then replaces the original value of the variable.
The values of two Strings can be compared using the String.Compare method. In addition, Strings can be compared using the standard arithmetic relational comparison operators (=, <>, <, >, <=, >=). Comparisons performed using the relational operators are always performed case sensitive, i.e. “A” is not equal to “a”. This is equivalent to specifying “Option Compare Binary” in some Basic compilers. To perform case insensitive comparisons, use the Compare method or force both String values to be upper or lower case.
Internally, String variables are implemented using many of the same procedures as those that apply to Objects. Consequently, many of the basic string manipulation operations are provided as methods and properties that can be applied to String variables. However, unlike other built-in Objects, when a String variable is created, it automatically has its data storage allocated. So, the use of the New qualifier is not needed in connections with String variables and is not permitted.
Table 19-8 summarizes each of the String methods and properties.
| Member | Type | Description |
|---|---|---|
|
String.Compare |
Method |
Compares the values of two Strings in either a case sensitive or case insensitive manner. |
|
string.IndexOf |
Method |
Searches for an exact match of a substring within the string variable and returns the starting position if found (0-n). |
|
string.Length |
Property |
Returns the number of characters in the String. |
|
string.Split |
Method |
Divides the string variable value into a series of substrings based upon a specified separator character and returns the array of substrings. |
|
string.Substring |
Method |
Returns a substring of the string variable starting at a specific character position and with a specified length. |
|
string.ToLower |
Method |
Returns a copy of the string with all lower case characters. |
|
string.ToUpper |
Method |
Returns a copy of the string with all upper case characters. |
|
string.Trim |
Method |
Trims off characters or white space from the start and end of a String variable value. |
|
string.TrimEnd |
Method |
Trims off characters or white space from the end of a String variable value. |
|
string.TrimStart |
Method |
Trims off characters or white space from the start of a String variable value. |
For compatibility with older Basic compilers, the following String functions are provided in Table 19-9. In many instances, very similar functionality is provided by the String Members listed in the previous table.
| Built-in String Functions | Description |
|---|---|
|
Asc(string) |
Converts the first character of a String to its equivalent ASCII numerical code. |
|
Chr(expression) |
Given a numerical ASCII code, a String that consists of the equivalent ASCII character code is returned. |
|
Format(expression, format_s) |
Converts a numerical value to a String value based upon a specified output format specification. |
|
FromBitString (string, type, big_endian) |
Extracts a number that has been packed in its internal bit format into a String and returns the value of the number. |
|
Instr(start,string_t, string_s) |
Searches for an exact match of a substring within a String expression and returns the starting position if found (1-n). |
|
LCase(string) |
Returns a String value that has been converted to lower case. |
|
Len(string) |
Returns the number of characters in a String. |
|
Mid(string, first, length) |
Returns a substring of the string starting at the first character position and consisting of length number of characters. |
|
ToBitString (expression, type, big_endian) |
Converts the value of an expression to a specific numeric type and returns the internal bit representation of the number packed into a String value. |
|
UCase(string) |
Returns a String value that has been converted to upper case. |