previous page next page reference home emBASIC home page

15 Built-in functions
Names for built-in exceptions and functions are found in a separate symbol table.
This table is searched last when the interpreter looks up the meaning of a name, so local and global user-defined names can override built-in names.
Built-in types are described together here for easy reference.
15.1 String functions
15.1.1 Bracket operator
Beside string functions that do look like functions a bracket operator exists. It is a shorthand notation for a function that selects a substring:
var[start:len]
var
is a string variable or a string component of complex variable. Note that is not expression.
start
is the start position. This might be a number or a string. If start is a number, it directly sets the start position (starting from 1);if the number is negative, the position is calculated from the end of string. Otherwise if start is a string, it is searched in the source string and the starting position will be the starting position of the result. The pattern may be prefixed with ``F''- to search forward or ``B'' (uppercase only) to search backward from the end of the string. “F” and „B“ are not part of the search string but the instruction to search for the first or last occurrence of the pattern.
len
length of the string returned. If len is #' the rest of string will be returned. A negative number reverses the return string.[WLG53]
For example, for string ``server is processing a very complex query''
string[10:10]. returns “processing”
string[-10:10]. returns “plex query”
string[-5:#]. returns “query”
string["ry":10]. returns “ry complex”,
string["Fry":10]. also returns “ry complex”
string["Bry":10]. returns “ry”

15.1.2 LEN
LEN(string AS STRING) AS INT
returns length of string:
LEN("very complex query") is 18

15.1.3 INDEX
INDEX(string AS STRING, substring AS STRING, N AS INT) AS INT
searches a string for N-th occurrence of substring and return starting position of it.
Examples:
INDEX("very complex query", "ry", 1) is 3
INDEX("very complex query", "ry", 2) is 17
INDEX("very complex query", "ry", 3) is 0
If N is negative, search is performed from tail.
INDEX("very complex query", "ry", -1) is also 17
15.1.4 FIELD
FIELD(string AS STRING, sep AS CHAR, N AS INT) AS INT
returns a field from a string.
sep
is a separator character
string
is a string to extract a field from
n
is a number of a field starting from 1. If N is negative then count fields from end.
Example:
FIELD("Aachen*Koeln*Berlin*Bonn", "*", -1) // returns “Bonn"

15.1.5 SWAP
SWAP(string AS STRING, substring AS STRING, replace AS STRING) AS STRING
replaces substrings in string and returns resulting string:
SWAP("very complex query", "ry", "+++") // “ve+++ complex que+++"

15.1.6 CONVERT
CONVERT(string AS STRING, set AS STRING, rep AS STRING) AS STRING
replaces in string symbols from set to corresponding symbols from rep.
CONVERT("very complex query", " zyx", "-abc") “verb-complec-querb"

15.1.7 TRIM
TRIM(string AS STRING) AS STRING
reduces blanks in a string: strips leading and terminating blanks, reduces sequences of intermediate blanks to a single space.
TRIM(" string with extra blanks ") // "string with extra blanks"
15.1.8 UPPERCASE
UPPERCASE(string AS STRING) AS STRING
returns string, with letters converted to upper case.
15.1.9 LOWERCASE
LOWERCASE(string AS STRING) AS STRING
returns string, with letters converted to lower case.
15.1.10 NAMECAPS
NAMECAPS(string AS STRING) AS STRING
returns string, with first character of each word (separated with blanks) converted to upper case.
15.1.11 INSERTFIELD
existingstr = INSERTFIELD(existingstr, newstr,delimiter, occurence)
The opposite to field. Inserts a string with the same delimiter after the occurrence specified.
string = INSERTFIELD(string, “HAMBURG”,”*”,0[WLG54][NM55])

/* returns “HAMBURG*AACHEN*KÖLN*BERLIN*BONN” */
15.1.12 LOCATE
Finds a string position in a sorted segmented string or a string array. Returns field/index where the string would go if it would be inserted. Options to specify sort order (AL = ascending left, AR = ascending right (numeric), DL = descending left, DR = descending right justified).
index = LOCATE(string, delimiter, order)
index = LOCATE(string[],order)
If the string or array isn’t sorted, the first valid position depending on its order would be returned.
15.1.13 SEQ
var = SEQ(char)
returns the ANSI sequence value. Example SEQ(“A”) returns 65
15.1.14 CHR$ and CHAR
Char = CHR$(int)
char = CHAR(int)
returns a character from the lower 8 bits of a signed or unsigned integer or from the equivalent value of a float.
15.1.15 FMT
string = FMT(format_string, expression)
Takes the expression and applies the format specified in format_string argument. Performs conversion depending on format string. Format string described it the “Output formatting” chaper[NM56].
15.1.16 [WLG57]STRFTIME[WLG58][NM59]
STRFTIME(STRING format, DATETIME time)
Performs output conversion for date and time according to system locale.
Currently takes LWORD time argument as result of MTIME function.
15.1.17 TIME_FORMAT
[WLG60][NM61]DECL tm AS DATETIME
tm = TIME_FORMAT(STRING format, STRING str)
Performs input conversion for date and time according to system locale.