Formulas 5. String functions

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\$)
gives the length of the string. After
```       string\$ = "hallo"``` ```       length = length (string\$ + "dag")```
the variable 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)
gives a string consisting of the first n characters of a\$. After
```       head\$ = left\$ ("hallo", 3)```
the variable head\$ contains the string "hal".
right\$ (a\$, n)
gives a string consisting of the last n characters of a\$. After
```       english\$ = "he" + right\$ ("hallo", 3)```
the variable english\$ contains the string "hello".
mid\$ ("hello" , 3, 2)
gives a string consisting of 2 characters from "hello", starting at the third character. Outcome: ll.
index (a\$, b\$)
gives the index of the first occurrence of the string b\$ in the string a\$. After
```       where = index ("hallo allemaal", "al")```
the variable 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\$)
gives the index of the last occurrence of the string b\$ in the string a\$. After
```       where = rindex ("hallo allemaal", "al")```
the variable 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\$)
determines whether the string a\$ starts with the string b\$. After
```       where = startsWith ("internationalization", "int")```
the variable where contains the number 1 (true).
endsWith (a\$, b\$)
determines whether the string a\$ ends with the string b\$. After
```       where = endsWith ("internationalization", "nation")```
the variable where contains the number 0 (false).
replace\$ (a\$, b\$, c\$, n)
gives a string that is like a\$, but where (at most n) occurrences of b\$ are replaced with the string c\$. After
```       s\$ = replace\$ ("hello", "l", "m", 0)```
the variable s\$ contains the string "hemmo". After
```       s\$ = replace\$ ("hello", "l", "m", 1)```
the variable 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\$)
determines where the string a\$ first matches the regular expression b\$. After
```       where = index_regex ("internationalization", "a.*n")```
the variable where contains the number 7. If there is no match, the outcome is 0.
rindex_regex (a\$, b\$)
determines where the string a\$ last matches the regular expression b\$. After
```       where = rindex_regex ("internationalization", "a.*n")```
the variable where contains the number 16. If there is no match, the outcome is 0.
replace_regex\$ (a\$, b\$, c\$, n)
gives a string that is like 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)```
the variable s\$ contains the string "hheelllloo". If there is no match, the outcome is the original string a\$. After
```       s\$ = replace_regex\$ ("hello", ".", "&&", 1)```
the variable 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)
formats a number as a string. Thus, `string\$ (5e6)` becomes the string `5000000`, and `string\$ (56%)` becomes the string `0.56`.
fixed\$ (number, precision)
formats a number as a string with precision digits after the decimal point. Thus, `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)
the same as 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\$)
interprets a string as a number.
```       string\$ = "5e6"``` ```       writeInfoLine: 3 + number (string\$)```
the Info window contains the number 500003.
date\$ ( )
gives the date and time in the following format:
```       Mon Jun 24 17:11:21 2002```
To write the day of the month into the Info window, you type:
```       date\$ = date\$ ()``` ```       day\$ = mid\$ (date\$, 9, 2)``` ```       writeInfoLine: "The month day is ", day\$, "."```
extractNumber ("Type: Sound" + newline\$ + "Name: hello there" + newline\$ + "Size: 44007", "Size:")
looks for a number after the first occurrence of "Size:" in the long string. Outcome: 44007. This is useful in scripts that try to get information from long reports, as the following script that runs in the Sound editor window:
```       report\$ = Editor info``` ```       maximumFrequency = extractNumber (report\$, "Spectrogram window length:")```
extractWord\$ ("Type: Sound" + newline\$ + "Name: hello there" + newline\$ + "Size: 44007", "Type:")
looks for a word without spaces after the first occurrence of "Type:" in the long string. Outcome: Sound.
extractLine\$ ("Type: Sound" + newline\$ + "Name: hello there" + newline\$ + "Size: 44007", "Name: ")
looks for the rest of the line (including spaces) after the first occurrence of "Name: " in the long string. Outcome: hello there. Note how "Name: " includes a space, so that the `rest of the line' starts with the h.
backslashTrigraphsToUnicode\$ (x\$), unicodeToBackslashTrigraphs\$ (x\$)
converts e.g. \ct to ɔ or the reverse. See Special symbols.