Navigation: Language Reference > 3 - Variable Declarations > Data Types >====== Implicit String Arrays and String Slicing ====== | |
In addition to their explicit declaration, all STRING, CSTRING and PSTRING variables have an implicit array declaration of one character strings, dimensioned by the length of the string. This is directly equivalent to declaring a second variable as:
StringVar STRING(10)
StringArray STRING(1),DIM(SIZE(StringVar)),OVER(StringVar)
This implicit array declaration allows each character in the string to be directly addressed as an array element, without the need of the second declaration. The PSTRING's length byte is addressed as element zero (0) of the array, as is the first byte of a BLOB (the only two cases in Clarion Win32 where an array has a zero element).
In the Clarion# language, all string arrays are zero-based. See examples below.
If the string also has a DIM attribute, this implicit array declaration is the last (optional) dimension of the array (to the right of the explicit dimensions). The MAXIMUM procedure does not operate on the implicit dimension, you should use SIZE instead.
You may also directly address multiple characters within a string using the “string slicing” technique. This technique performs a similar function to the SUB procedure, but is much more flexible and efficient (but does no bounds checking). It is more flexible because a “string slice” may be used as either the destination or source sides of an assignment statement, while the SUB procedure can only be used as the source. It is more efficient because it takes less memory than either individual character assignments or the SUB procedure.
To take a “slice” of the string, the beginning and ending character numbers are separated by a colon (:) and placed in the implicit array dimension position within the square brackets ([]) of the string. The position numbers may be integer constants, variables, or expressions (internally computed as LONG base type). If variables are used, there must be at least one blank space between the variable name and the colon separating the beginning and ending number (to prevent PREfix confusion).
Individual elements may be cleared with the use of the CLEAR statement. For example:
CLEAR( MyString[ n : m ] ) or CLEAR( MyString[ n : m ], 0 ) for clear with spaces ( 20h )
CLEAR( MyString[ n : m ], -1 ) for clear with lowest value ( 00h )
CLEAR( MyString[ n : m ] , 1 ) for clear with highest value ( 0FFh )
Clarion (Win32) Example:
Name STRING(15)
CONTACT STRING(15),DIM(4)
Five EQUATE(5)
Six EQUATE(6)
CODE
Name = 'Tammi' ! Assign a value
Name[5] = 'y' ! then change fifth letter
Name[6] = 's' ! then add a letter
Name[0] = '<;6>' ! and handle length byte if Name was a PSTRING
Name[5:6] = 'ie' ! and change a “slice” – the fifth and sixth letters
Name[Five : Six] ! need a space between equates and variables
Contact[1] = 'First' ! Assign value to first element
Contact[1,2] = 'u' ! Change first element 2nd character
Contact[1,2:3] = Name[5:6] ! Assign slice to first element 2nd & 3rd characters
Clarion# (Clarion.NET) Example:
CODE |
ns = 'abcdefghijklmnopqrstuvwxyz' |
cs = ns |
! all of the following are expected, at position zero is the first character |
i# = 0 |
SUB(ns,i#,1) !returns 'a' |
SUB(cs,i#,1) !returns 'a' |
ns[i#] !returns 'a' (slice) |
cs[i#] !returns 'a' (slice) |
See Also: