Text and Strings

Word Action
SAME? 
( "s1" "s2" -- TRUE | FALSE )
Test for string equality.
INDEXOF 
( "s1" "s2" -- n | -1 )
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 
( "s1" "s2" k -- n | -1 )
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 
( "s1" "s2" -- n | -1 )
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 
( "s1" char -- n | -1 )
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 
( "s1" char k -- n | -1 )
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 
( "s1" char -- n | -1 )
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 
( "s" k -- "s" )
Takes a string and an integer as arguments. Returns the substring of the string starting from index k until the end.
SUBSTRING2 
( "s" m n -- "s" )
Takes a string and two integers as arguments. Returns the substring of the string starting from index m until index n-1.
PREFIX? 
( "s1" "s2" -- f )
Takes two strings as arguments. Returns
TRUE
if the first string starts with the second string,
FALSE
if otherwise.
SUFFIX? 
( "s1" "s2" -- f )
Takes two strings as arguments. Returns
TRUE
if the first string ends with the second string,
FALSE
if otherwise.
TRIM 
( "s" -- "s'" )
Takes a string as an argument. Returns another string where all the leading and trailing white spaces in the original string is removed.
LENGTH 
( "s" -- n )
Takes a string as an argument. Returns the length of the string.
SPLIT 
( "s" "delimiter" -- "s1" "s2" ... "sN" N )
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 
( "s" -- n )
Takes a string which consists only numbers as an argument.
INT
converts the string into an integer.
[char] 
( |s -- char )
[char]
is used to create a character. For example,
[char] \n
will create a carriage return character
\n
and push it on the stack.
GSUB 
( "text" "pattern" "replacement" -- "text" )
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 
( "text1" "text2" -- "text1text2" )
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 using 
INDEXOF
and
INDEXOFC
? Which is more efficient?



Question 2

Suppose we want to define complex numbers. For example, 3 + 4i can be written as 
3::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".



Question 3

Will the following work?

    "non-integer" INT 


Question 4

Write a word 
COUNT
that 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.



Next: Time