|
String functions are functions that either return a text string or have at least one text string as an argument. Since string computations are not very useful in the calculator, in settings windows, or in creation and modification formulas, this page only gives examples of strings in scripts, so that the example may contain string variables.
length (a$)
string$ = "hallo"
length = length (string$ + "dag")
length
contains the number 8 (by the way, from this example you see that variables can have the same names as functions, without any danger of confusing the interpreter).
left$ (a$, n)
a$
. After
head$ = left$ ("hallo", 3)
head$
contains the string "hal".
right$ (a$, n)
a$
. After
english$ = "he" + right$ ("hallo", 3)
english$
contains the string "hello".
mid$ ("hello" , 3, 2)
index (a$, b$)
b$
in the string a$
. After
where = index ("hallo allemaal", "al")
where
contains the number 2, because the first “al” starts at the second character of the longer string. If the first string does not contain the second string, index
returns 0.
rindex (a$, b$)
b$
in the string a$
. After
where = rindex ("hallo allemaal", "al")
where
contains the number 13, because the last “al” starts at the 13th character. If the first string does not contain the second string, rindex
returns 0.
startsWith (a$, b$)
a$
starts with the string b$
. After
where = startsWith ("internationalization", "int")
where
contains the number 1 (true).
endsWith (a$, b$)
a$
ends with the string b$
. After
where = endsWith ("internationalization", "nation")
replace$ (a$, b$, c$, n)
a$
, but where (at most n) occurrences of b$
are replaced with the string c$
. After
s$ = replace$ ("hello", "l", "m", 0)
s$
contains the string "hemmo". After
s$ = replace$ ("hello", "l", "m", 1)
s$
contains the string "hemlo". The number n determines the maximum number of occurrences of b$
that can be replaced. If n is 0, all occurrences are replaced.
index_regex (a$, b$)
a$
first matches the regular expression b$
. After
where = index_regex ("internationalization", "a.*n")
where
contains the number 7. If there is no match, the outcome is 0.
rindex_regex (a$, b$)
a$
last matches the regular expression b$
. After
where = rindex_regex ("internationalization", "a.*n")
where
contains the number 16. If there is no match, the outcome is 0.
replace_regex$ (a$, b$, c$, n)
a$
, but where (at most n) substrings that match the regular expression b$
are replaced with the expression c$
. After
s$ = replace_regex$ ("hello", ".", "&&", 0)
s$
contains the string "hheelllloo". If there is no match, the outcome is the original string a$
. After
s$ = replace_regex$ ("hello", ".", "&&", 1)
s$
contains the string "hhello". The number n determines the maximum number of text pieces that can be replaced. If n is 0, all matching text pieces are replaced.
string$ (number)
string$ (5e6)
becomes the string 5000000
, and string$ (56%)
becomes the string 0.56
.
fixed$ (number, precision)
fixed$ (72.65687, 3)
becomes the string 72.657
, and fixed$ (72.65001, 3)
becomes the string 72.650
. In these examples, we see that the result can be rounded up and that trailing zeroes are kept. At least one digit of precision is always given, e.g. fixed$ (0.0000157, 3)
becomes the string 0.00002
. The number 0 always becomes the string 0
.
percent$ (number, precision)
fixed$
(), but with a percent sign. For instance, percent$ (0.157, 3)
becomes 15.700%
, percent$ (0.000157, 3)
becomes 0.016%
, and percent$ (0.000000157, 3)
becomes 0.00002%
. The number 0 always becomes the string 0
.
number (a$)
string$ = "5e6"
writeInfoLine: 3 + number (string$)
date$ ( )
Mon Jun 24 17:11:21 2002
date$ = date$ ()
day$ = mid$ (date$, 9, 2)
writeInfoLine: "The month day is ", day$, "."
unicode$ (228)
unicode ("ä")
extractNumber ("Type: Sound" + newline$ + "Name: hello there" + newline$ + "Size: 44007", "Size:")
report$ = Editor info
maximumFrequency = extractNumber (report$, "Spectrogram window length:")
extractWord$ ("Type: Sound" + newline$ + "Name: hello there" + newline$ + "Size: 44007", "Type:")
extractLine$ ("Type: Sound" + newline$ + "Name: hello there" + newline$ + "Size: 44007", "Name: ")
backslashTrigraphsToUnicode$ (x$), unicodeToBackslashTrigraphs$ (x$)
© ppgb 20180825