Lesson 6: Strings
Strings are sequences of characters. Specifically a string is any sequence of ASCII characters (each character can have a length identical to char-size), and ending with the literal value 0.
Strings are created by parsing, or can be manually constructed as an array. For example the following two are functionally identical:
" hello" is-data hello
hello type cr
5 chars is-array hello
char: h 0 hello array.putChar
char: e 1 hello array.putChar
char: l 2 hello array.putChar
char: l 3 hello array.putChar
char: o 4 hello array.putChar
0 5 hello array.putChar
hello type cr
Since all strings are arrays, you can also manipulate individual elements in a string:
" hello" is-data hello hello type cr #! Now change the lowercase 'h' to uppercase. char: H 0 hello array.putChar hello type cr
Tips
- char-size is normally equal to 1, corresponding to one byte.
- It is possible (though not likely) to have a char-size larger than one byte.
- Use c@ and c! when manipulating char-size elements.
- Do not use @ and ! (which are for cell-size elements).
Some operations on strings are pretty common. Toka provides a set of functions for dealing with these.
These are string.getLength, string.grow, string.append, string.appendChar, string.clone, and string.compare. Additional string related words are provided in the strings library.
Some examples of using these words:
" Hello" string.getLength .