There are a number of useful built-in functions which manipulate strings. Remember that an E-string can be used wherever a normal string can, but normal strings cannot be used where an E-string is required. If a parameter is marked as string then a normal or E-string can be passed as that parameter, but if it is marked as e-string then only an E-string may be used. Some of these functions have default arguments, which means you don't need to specify some parameters to get the default values (see 7.3 Default Arguments). (You can, of course, ignore the defaults and always give all parameters.)
String(maxsize)
STRING declaration does.
The following code fragments are practically equivalent:
The slight difference is that there may not be enough memory left to hold the E-string when theDEF s[37]:STRING DEF s:PTR TO CHAR s:=String(37)
String function is used.
In that case the special value NIL (a constant) is returned.
Your program must check that the value returned is not NIL before you use it as an E-string (or dereference it).
The memory for the declaration version, STRING, is allocated when the program is run, so your program won't run if there isn't enough memory.
The String version is often called dynamic allocation because it happens only when the program is running; the declaration version has allocation done by the E compiler.
The memory allocated using String can be deallocated using DisposeLink (see 11.3.5 System support functions).
StrCmp(string1,string2,length=ALL)
TRUE if the first length characters of the strings match, and FALSE otherwise.
The length defaults to the special constant ALL which means that the strings must agree on every character.
For example, the following comparisons all return TRUE:
StrCmp('ABC', 'ABC')
StrCmp('ABC', 'ABC', ALL)
StrCmp('ABCd', 'ABC', 3)
StrCmp('ABCde','ABCxxjs',3)
And the following return FALSE (notice the case of the letters):
StrCmp('ABC', 'ABc')
StrCmp('ABC', 'ABc', ALL)
StrCmp('ABCd', 'ABC', ALL)
StrCopy(e-string,string,length=ALL)
ALL can be used to indicate that the whole of the source string is to be copied (and this is the default value for length).
Remember that E-strings are safely manipulated, so the following code fragment results in s becoming `More th', since its maximum size is (from its declaration) seven characters.
A declaration usingDEF s[7]:STRING StrCopy(s, 'More than seven characters', ALL)
STRING (or ARRAY) reserves a small part of memory, and stores a pointer to this memory in the variable being declared.
So to get data into this memory you need to copy it there, using StrCopy.
If you're familiar with very high-level languages like BASIC you should take care, because you might think you can assign a string to an array or an E-string variable.
In E (and languages like C and Assembly) you must explicitly copy data into arrays and E-strings.
You should not do the following:
This is fairly disastrous: it throws away the pointer to reserved memory that was stored in/* You don't want to do things like this! */ DEF s[80]:STRING s:='This is a string constant'
s and replaces it by a pointer to the string constant.
s is then no longer an E-string, and cannot be repaired using SetStr.
If you want s to contain the above string you must use StrCopy:
The moral is: remember when you are using pointers to data and when you need to copy data. Also, remember that assignment does not copy large arrays of data, it copies only pointers to data, so if you want to store some data in anDEF s[80]:STRING StrCopy(s,'This is a string constant')
ARRAY or STRING type variable you need to copy it there.
StrAdd(e-string,string,length=ALL)
StrCopy but the source string is copied onto the end of the destination E-string.
The following code fragment results in s becoming `This is a string and a half'.
DEF s[30]:STRING StrCopy(s, 'This is a string', ALL) StrAdd(s, ' and a half')
StrLen(string)
StrLen('abc')
StrLen('abc\0def')
In fact, most of the string functions assume strings are null-terminated, so you shouldn't use null characters in your strings unless you really know what you're doing.
For E-strings StrLen is less efficient than the EstrLen function.
EstrLen(e-string)
StrLen since E-strings know their length and it doesn't need to search the string for a null character.
StrMax(e-string)
STRING or the call to String.
RightStr(e-string1,e-string2,length)
StrCopy but it copies the right-most characters from e-string2 to e-string1 and both strings must be E-strings.
At most length characters are copied, and the special constant ALL cannot be used (to copy all the string you should, of course, use StrCopy).
For instance, a value of one for length means the last character of e-string2 is copied to e-string1.
MidStr(e-string,string,index,length=ALL)
ALL can be used if all the remaining characters in string should be copied (this is the default value for length).
For example, the following two calls to MidStr result in s becoming `four':
DEF s[30]:STRING MidStr(s, 'Just four', 5) MidStr(s, 'Just four apples', 5, 4)
InStr(string1,string2,startindex=0)
TrimStr(string)
s becoming `12345'.
DEF s:PTR TO CHAR
s:=TrimStr(' \n \t 12345')
LowerStr(string)
UpperStr(string)
SetStr(e-string,length)
Notice that this function can be used to shorten an E-string, but this change is destructive (it cannot easily be reversed to give the original, longer E-string).DEF s[10]:STRING s[0]:="a" /* Remember that "a" is a character value. */ s[1]:="b" s[2]:="c" s[3]:="d" /* At this point s is just an array of CHAR. */ SetStr(s, 4) /* Now, s can be used as an E-string again. */ SetStr(s, 2) /* s is a bit shorter, but still an E-string.*/
Val(string,address=NIL)
LONG integer.
Leading whitespace is ignored, and a leading `%' or `$' means that the string denotes a binary or hexadecimal integer (in the same way they do for numeric constants).
The decoded integer is returned as the regular return value (see 7.4 Multiple Return Values).
The number of characters of string that were read to make the integer is stored at address, which is usually a variable address (from using {var}), and is also returned as the first optional return value.
If address is the special constant NIL (i.e., zero) then this number is not stored (this is the default value for address).
You can use this number to calculate the position in the string which was not part of the integer.
If an integer could not be decoded from the string then zero is returned as both return values and stored at address.
Follow the comments in this example, and pay special attention to the use
of the pointer p.
DEF s[30]:STRING, value, chars, p:PTR TO CHAR
StrCopy(s, ' \t \n 10 \t $3F -%0101010')
value, chars:=Val('abcde 10 20') -> Two return values...
/* After the above line, value and chars will both be zero */
value:=Val(s, {chars}) -> Use address of chars
/* Now value will be 10, chars will be 7 */
p:=s+chars
/* p now points to the space after the 10 in s */
value, chars:=Val(p)
/* Now value will be $3F (63), chars will be 6 */
p:=p+chars
/* p now points to the space after the $3F in s */
value, chars:=Val(p)
/* Now value will be -%0101010 (-42), chars will be 10 */
Notice the two different ways of finding the number of characters read: a multiple-assignment and using the address of a variable.
There's a couple of other string functions (ReadStr and StringF) which will be discussed later (see 11.3.1 Input and output functions).
Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.