String Functions
 
FB Logo

Built-in String Functions
    CHR
    HEX
    LEFT
    LEN
    RIGHT
    STR
    STRCOMP
    VAL

VBSTRING.bas Library (VB style)
    MID
    INSTR
    UCASE
    LCASE

STRING.bas Library (C style)
    MIDSTR
    STRCHR
    STRSTR
    TOLOWER
    TOUPPER


String functions may not be nested.  What does this mean?
 
String functions are built using a string accumulator which is a 256 byte buffer.  There is only one string accumulator due to memory constraints.  The general expression evaluation for integers involves a stack, but it is impractical to have a string stack.  So when a string is built from an expression, it uses this string accumulator.  String FUNCTIONs also use this string accumulator to return the string value.  So string FUNCTIONs can not be used after the first operand in a string expression.
 
As there is only the single string accumulator, string functions should NOT be performed inside INTERRUPT SUB. 
 
String expressions are parsed left to right, and parenthesis for grouping are not allowed as that is the equivalent of nesting.  However a string expression can have any number of strings being combined into a single string.  So the following is proper-
 
DIM ast(30) as string
DIM bst(30) as string
DIM cst(30) as string
 
ast = ast + "abcd" + str(2 + 44 / 33) + str(len(cst)) + "zcxv" + chr(13) + "more stuff" + bst
 
The chr(13) inserts a carriage return into this string so it spans 2 lines.  This is proper as strings only have two limitations.  First that they are less that 256 bytes, and they are terminated by a 0 or null character.
 
Note that the str(2 + 44 / 33) involves the integer evaluation stack and is OK as that is a separate entity.  Also the str(len(cst)) is valid as that involves a string as stored in memory. 
 
What would not be allowed is something like
 
ast = "length is " + str(len( cst + bst))       '  THIS IS INVALID NESTING
 
because cst + bst would have to be evaluated before ast could be built, and there is no room to do that. 
 
ast = "length is " + str(len(cst) + len(bst))      ' allowed as len is called with simple pointers
 
 
User FUNCTIONs
 
Now with the addition of user defined functions, there is the possibility of a nested string function that the compiler can not detect.  If a string expression calls a user function, and that user function does ANY string expressions or PRINT statements; then this is a nested string operation.  The compiler will not be able to detect this, and its possible to get unexpected string results or even data abort errors.
 
ast = user_string_function (1,2,3)                ' is OK
 
ast = str (user_integer_function (1,2,3))       ' is OK
 
ast = "result of " + user_string_function (1,2,3)           ' INVALID string nesting
 
ast = "result of "+ str(user_integer_function (1,2,3))    ' valid only if no string op or PRINT statement in user_integer_function
 
ast = user_string_function (1,2,3) + " returned"           ' is OK, as the string function was the first called
 
ast = str(user_int_function (1,2,3)) + " returned"          ' is OK, as the user function was the first called
 
 
VB vs. C style String Functions
 
VB accesses the first character by Stringname.Chars(0) In ARMbasic that first character is accessed by Stringname(0)

But VB's MID ("This is a string",1,3) returns "Thi".

The existing library of string functions was translated from C, which is always 0 based for the first element. So

MIDSTR ("This is a string",1,3) returns "his"