Text and Strings
Word | Action |
---|---|
SAME? |
Test for string equality. |
INDEXOF |
Takes two strings as arguments. Returns the index of the first occurence of the second string in the first string. Returns -1 if does not exists. |
INDEXOF2 |
Takes two strings and an integer as arguments. Returns the index of the occurence of the second string from index k in the first string. Returns -1 if does not exists. |
LASTINDEXOF |
Takes two strings as arguments. Returns the index of the last occurence of the second string in the first string. Returns -1 if does not exists. |
INDEXOFC |
Takes a string and a character as arguments. Returns the index of the first occurence of the character in the string. Returns -1 if does not exists. |
INDEXOFC2 |
Takes a string, a character and an integer as arguments. Returns the index of the occurence of the character from index k in the string. Returns -1 if does not exists. |
LASTINDEXOFC |
Takes a string and a character as arguments. Returns the index of the last occurence of the character in the string. Returns -1 if does not exists. |
SUBSTRING |
Takes a string and an integer as arguments. Returns the substring of the string starting from index k until the end. |
SUBSTRING2 |
Takes a string and two integers as arguments. Returns the substring of the string starting from index m until index n-1. |
PREFIX? |
Takes two strings as arguments. Returns TRUEif the first string starts with the second string, FALSEif otherwise. |
SUFFIX? |
Takes two strings as arguments. Returns TRUEif the first string ends with the second string, FALSEif otherwise. |
TRIM |
Takes a string as an argument. Returns another string where all the leading and trailing white spaces in the original string is removed. |
LENGTH |
Takes a string as an argument. Returns the length of the string. |
SPLIT |
Takes two strings as arguments. The first string act as the original string while the second string act as a delimiter. The original string will be seperated everytime the delimiter is encountered. The delimiter will be dropped from the string. Returns all the strings that are splited and the number of strings resulted. |
INT |
Takes a string which consists only numbers as an argument. INTconverts the string into an integer. |
[char] |
[char]is used to create a character. For example, [char] \nwill create a carriage return character \nand push it on the stack. |
GSUB |
Replaces every occurance of the given pattern in the text with the replacement. For example,
"Hello World" "Hello" "Hi" GSUB .will print Hi World |
CONCAT |
Concatenates two pieces of text. For example,
"Hello" "World" CONCAT .will print HelloWorld |
Examples
"reference" "e" indexof . 1 ok "reference" "e" 2 indexof2 . 3 ok "reference" "e" lastindexof . 8 ok "reference" [char] e indexofc . 1 ok "hello" 3 substring . lo ok "hello" 1 3 substring2 . el ok "hello" "he" prefix? . true ok "hello" "he" suffix? . false ok "hello" "lo" suffix? . true ok " bravo \t " trim . bravo ok " bravo \t " length . 10 ok " bravo \t " trim length . 5 ok "a/b/c/d/e" "/" split .s <6> a b c d e 5 ok
Quiz
Question 1
What is the difference of usingINDEXOFand
INDEXOFC? Which is more efficient?
INDEXOFserach for a substring while
INDEXOFCsearch for a single character. When the substring contains one character only, it is more efficient to use
INDEXOFC.
Question 2
Suppose we want to define complex numbers. For example, 3 + 4i can be written as3::4. Write a word that takes in a string with the format "\d::\d" and prints out the string in the form of "\d + \d i".
: printComplex ( "\d::\d" -- ) \ split real and imaginary part, discard the final number returned by split "::" split drop \ imaginary part int >R \ real part int \ printing . "+" . R> . "i" . ;
Question 3
Will the following work?
"non-integer" INT
No,
INTcan convert a string containing integer only.
Question 4
Write a wordCOUNTthat takes a string and a character as arguments and returns the number of occurence of the character in the string. You may need to use looping constructs.
: COUNT ( "s" char -- d ) 0 0 >R >R begin over over R> indexofc2 R> 1 + >R dup 1 + >R -1 = until R> drop R> 1 - ; "aaba" [char] a count . \ 3 "nothing" [char] z count . \ 0 "" [char] n count . \ 0 "reference" [char] e count . \ 4