| 1 | <html><head><meta name="robots" content="index,follow"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| 2 | <title>Formulas 6. String functions</title> |
| 3 | <style> |
| 4 | td { padding-left: 5pt; padding-right: 5pt; } |
| 5 | th { padding-left: 5pt; padding-right: 5pt; } |
| 6 | code { white-space: pre-wrap; } |
| 7 | dd { white-space: pre-wrap; } |
| 8 | </style> |
| 9 | </head><body bgcolor="#FFFFFF"> |
| 10 | |
| 11 | <table border=0 cellpadding=0 cellspacing=0><tr><td bgcolor="#CCCC00"><table border=4 cellpadding=9><tr><td align=middle bgcolor="#000000"><font face="Palatino,Times" size=6 color="#999900"><b> |
| 12 | Formulas 6. String functions |
| 13 | </b></font></table></table> |
| 14 | <p>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 <a href="Calculator.html">calculator</a>, 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.</p> |
| 15 | <dl> |
| 16 | <dt><b><code><font size=+1>length (a$)</font></code></b> |
| 17 | <dd>gives the length of the string. After</dd> |
| 18 | </dl> |
| 19 | <code> string$ = "hallo"<br></code> |
| 20 | <code> length = length (string$ + "dag")<br></code> |
| 21 | <dl> |
| 22 | <dd>the variable <i><code><font size=+1>length</font></code></i> 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).</dd> |
| 23 | <dt><b><code><font size=+1>left$ (a$, n)</font></code></b> |
| 24 | <dd>gives a string consisting of the first <i>n</i> characters of <i><code><font size=+1>a$</font></code></i>. After</dd> |
| 25 | </dl> |
| 26 | <code> head$ = left$ ("hallo", 3)<br></code> |
| 27 | <dl> |
| 28 | <dd>the variable <i><code><font size=+1>head$</font></code></i> contains the string "hal".</dd> |
| 29 | <dt><b><code><font size=+1>right$ (a$, n)</font></code></b> |
| 30 | <dd>gives a string consisting of the last <i>n</i> characters of <i><code><font size=+1>a$</font></code></i>. After</dd> |
| 31 | </dl> |
| 32 | <code> english$ = "he" + right$ ("hallo", 3)<br></code> |
| 33 | <dl> |
| 34 | <dd>the variable <i><code><font size=+1>english$</font></code></i> contains the string "hello".</dd> |
| 35 | <dt><b><code><font size=+1>mid$ ("hello" , 3, 2)</font></code></b> |
| 36 | <dd>gives a string consisting of 2 characters from “hello”, starting at the third character. Outcome: ll.</dd> |
| 37 | <dt><b><code><font size=+1>index (a$, b$)</font></code></b> |
| 38 | <dd>gives the index of the first occurrence of the string <i><code><font size=+1>b$</font></code></i> in the string <i><code><font size=+1>a$</font></code></i>. After</dd> |
| 39 | </dl> |
| 40 | <code> where = index ("hallo allemaal", "al")<br></code> |
| 41 | <dl> |
| 42 | <dd>the variable <i><code><font size=+1>where</font></code></i> 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, <i><code><font size=+1>index</font></code></i> returns 0.</dd> |
| 43 | <dt><b><code><font size=+1>rindex (a$, b$)</font></code></b> |
| 44 | <dd>gives the index of the last occurrence of the string <i><code><font size=+1>b$</font></code></i> in the string <i><code><font size=+1>a$</font></code></i>. After</dd> |
| 45 | </dl> |
| 46 | <code> where = rindex ("hallo allemaal", "al")<br></code> |
| 47 | <dl> |
| 48 | <dd>the variable <i><code><font size=+1>where</font></code></i> contains the number 13, because the last “al” starts at the 13th character. If the first string does not contain the second string, <i><code><font size=+1>rindex</font></code></i> returns 0.</dd> |
| 49 | <dt><b><code><font size=+1>startsWith (a$, b$)</font></code></b> |
| 50 | <dd>determines whether the string <i><code><font size=+1>a$</font></code></i> starts with the string <i><code><font size=+1>b$</font></code></i>. After</dd> |
| 51 | </dl> |
| 52 | <code> where = startsWith ("internationalization", "int")<br></code> |
| 53 | <dl> |
| 54 | <dd>the variable <i><code><font size=+1>where</font></code></i> contains the number 1 (true).</dd> |
| 55 | <dt><b><code><font size=+1>endsWith (a$, b$)</font></code></b> |
| 56 | <dd>determines whether the string <i><code><font size=+1>a$</font></code></i> ends with the string <i><code><font size=+1>b$</font></code></i>. After</dd> |
| 57 | </dl> |
| 58 | <code> where = endsWith ("internationalization", "nation")<br></code> |
| 59 | <dl> |
| 60 | <dd>the variable <i>where</i> contains the number 0 (false).</dd> |
| 61 | <dt><b><code><font size=+1>replace$ (a$, b$, c$, n)</font></code></b> |
| 62 | <dd>gives a string that is like <i><code><font size=+1>a$</font></code></i>, but where (at most <i>n</i>) occurrences of <i><code><font size=+1>b$</font></code></i> are replaced with the string <i><code><font size=+1>c$</font></code></i>. After</dd> |
| 63 | </dl> |
| 64 | <code> s$ = replace$ ("hello", "l", "m", 0)<br></code> |
| 65 | <dl> |
| 66 | <dd>the variable <i><code><font size=+1>s$</font></code></i> contains the string "hemmo". After</dd> |
| 67 | </dl> |
| 68 | <code> s$ = replace$ ("hello", "l", "m", 1)<br></code> |
| 69 | <dl> |
| 70 | <dd>the variable <i><code><font size=+1>s$</font></code></i> contains the string "hemlo". The number <i>n</i> determines the maximum number of occurrences of <i><code><font size=+1>b$</font></code></i> that can be replaced. If <i>n</i> is 0, all occurrences are replaced.</dd> |
| 71 | <dt><b><code><font size=+1>index_regex (a$, b$)</font></code></b> |
| 72 | <dd>determines where the string <i><code><font size=+1>a$</font></code></i> first matches the <a href="Regular_expressions.html">regular expression</a> <i><code><font size=+1>b$</font></code></i>. After</dd> |
| 73 | </dl> |
| 74 | <code> where = index_regex ("internationalization", "a.*n")<br></code> |
| 75 | <dl> |
| 76 | <dd>the variable <i><code><font size=+1>where</font></code></i> contains the number 7. If there is no match, the outcome is 0.</dd> |
| 77 | <dt><b><code><font size=+1>rindex_regex (a$, b$)</font></code></b> |
| 78 | <dd>determines where the string <i><code><font size=+1>a$</font></code></i> last matches the <a href="Regular_expressions.html">regular expression</a> <i><code><font size=+1>b$</font></code></i>. After</dd> |
| 79 | </dl> |
| 80 | <code> where = rindex_regex ("internationalization", "a.*n")<br></code> |
| 81 | <dl> |
| 82 | <dd>the variable <i><code><font size=+1>where</font></code></i> contains the number 16. If there is no match, the outcome is 0.</dd> |
| 83 | <dt><b><code><font size=+1>replace_regex$ (a$, b$, c$, n)</font></code></b> |
| 84 | <dd>gives a string that is like <i><code><font size=+1>a$</font></code></i>, but where (at most <i>n</i>) substrings that match the <a href="Regular_expressions.html">regular expression</a> <i><code><font size=+1>b$</font></code></i> are replaced with the expression <i><code><font size=+1>c$</font></code></i>. After</dd> |
| 85 | </dl> |
| 86 | <code> s$ = replace_regex$ ("hello", ".", "&&", 0)<br></code> |
| 87 | <dl> |
| 88 | <dd>the variable <i><code><font size=+1>s$</font></code></i> contains the string "hheelllloo". If there is no match, the outcome is the original string <i><code><font size=+1>a$</font></code></i>. After</dd> |
| 89 | </dl> |
| 90 | <code> s$ = replace_regex$ ("hello", ".", "&&", 1)<br></code> |
| 91 | <dl> |
| 92 | <dd>the variable <i><code><font size=+1>s$</font></code></i> contains the string "hhello". The number <i>n</i> determines the maximum number of text pieces that can be replaced. If <i>n</i> is 0, all matching text pieces are replaced.</dd> |
| 93 | <dt><b><code><font size=+1>string$ (number)</font></code></b> |
| 94 | <dd>formats a number as a string. Thus, <code><font size=+1>string$ (5e6)</font></code> becomes the string <code><font size=+1>5000000</font></code>, and <code><font size=+1>string$ (56%)</font></code> becomes the string <code><font size=+1>0.56</font></code>.</dd> |
| 95 | <dt><b><code><font size=+1>fixed$ (number, precision)</font></code></b> |
| 96 | <dd>formats a number as a string with <i>precision</i> digits after the decimal point. Thus, <code><font size=+1>fixed$ (72.65687, 3)</font></code> becomes the string <code><font size=+1>72.657</font></code>, and <code><font size=+1>fixed$ (72.65001, 3)</font></code> becomes the string <code><font size=+1>72.650</font></code>. 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. <code><font size=+1>fixed$ (0.0000157, 3)</font></code> becomes the string <code><font size=+1>0.00002</font></code>. The number 0 always becomes the string <code><font size=+1>0</font></code>.</dd> |
| 97 | <dt><b><code><font size=+1>percent$ (number, precision)</font></code></b> |
| 98 | <dd>the same as <code><font size=+1>fixed$</font></code> (), but with a percent sign. For instance, <code><font size=+1>percent$ (0.157, 3)</font></code> becomes <code><font size=+1>15.700%</font></code>, <code><font size=+1>percent$ (0.000157, 3)</font></code> becomes <code><font size=+1>0.016%</font></code>, and <code><font size=+1>percent$ (0.000000157, 3)</font></code> becomes <code><font size=+1>0.00002%</font></code>. The number 0 always becomes the string <code><font size=+1>0</font></code>.</dd> |
| 99 | <dt><b><code><font size=+1>number (a$)</font></code></b> |
| 100 | <dd>interprets a string as a number. After</dd> |
| 101 | </dl> |
| 102 | <code> string$ = "5e6"<br></code> |
| 103 | <code> writeInfoLine: 3 + number (string$)<br></code> |
| 104 | <dl> |
| 105 | <dd>the Info window contains the number 5000003.</dd> |
| 106 | <dt><b><code><font size=+1>date$ ( )</font></code></b> |
| 107 | <dd>gives the date and time in the following format:</dd> |
| 108 | </dl> |
| 109 | <code> Mon Jun 24 17:11:21 2002<br></code> |
| 110 | <dl> |
| 111 | <dd>To write the day of the month into the Info window, you type:</dd> |
| 112 | </dl> |
| 113 | <code> date$ = date$ ()<br></code> |
| 114 | <code> day$ = mid$ (date$, 9, 2)<br></code> |
| 115 | <code> writeInfoLine: "The month day is ", day$, "."<br></code> |
| 116 | <dl> |
| 117 | <dt><b><code><font size=+1>unicode$ (228)</font></code></b> |
| 118 | <dd>gives the 228th Unicode codepoint, i.e. "ä".</dd> |
| 119 | <dt><b><code><font size=+1>unicode ("ä")</font></code></b> |
| 120 | <dd>gives the Unicode codepoint number of "ä", i.e. 228.</dd> |
| 121 | <dt><b><code><font size=+1>extractNumber ("Type: Sound" + newline$ + "Name: hello there" + newline$ + "Size: 44007", "Size:")</font></code></b> |
| 122 | <dd>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:</dd> |
| 123 | </dl> |
| 124 | <code> report$ = Editor info<br></code> |
| 125 | <code> maximumFrequency = extractNumber (report$, "Spectrogram window length:")<br></code> |
| 126 | <dl> |
| 127 | <dt><b><code><font size=+1>extractWord$ ("Type: Sound" + newline$ + "Name: hello there" + newline$ + "Size: 44007", "Type:")</font></code></b> |
| 128 | <dd>looks for a word without spaces after the first occurrence of "Type:" in the long string. Outcome: Sound.</dd> |
| 129 | <dt><b><code><font size=+1>extractLine$ ("Type: Sound" + newline$ + "Name: hello there" + newline$ + "Size: 44007", "Name: ")</font></code></b> |
| 130 | <dd>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 <i>h</i>.</dd> |
| 131 | <dt><b><code><font size=+1>backslashTrigraphsToUnicode$ (x$), unicodeToBackslashTrigraphs$ (x$)</font></code></b> |
| 132 | <dd>converts e.g. \ct to ɔ or the reverse. See <a href="Special_symbols.html">Special symbols</a>.</dd> |
| 133 | </dl> |
| 134 | <h3>Links to this page</h3> |
| 135 | <ul> |
| 136 | <li><a href="Formulas.html">Formulas</a> |
| 137 | <li><a href="What_was_new_in_4_1_.html">What was new in 4.1?</a> |
| 138 | <li><a href="What_was_new_in_4_5_.html">What was new in 4.5?</a> |
| 139 | </ul> |
| 140 | <hr> |
| 141 | <address> |
| 142 | <p>© ppgb 20180825</p> |
| 143 | </address> |
| 144 | </body> |
| 145 | </html> |