8.16 Appending strings. 4tH 4tH Forth compiler
Advertisement
Advertisement
CHAPTER 8. 4TH ARRAYS
If we want to discard the first name at all we could even write:
32 string one s" Hans Bezemer" one place
\ define string one
\ initialize string one one count 5 /string type cr \ print sliced string
84
The five characters we want to skip are the first name (which is four characters) and a space
(which adds up to five). There is a special word for slicing strings in the library member slice.4th
. You call it with: address count position-to-start position-to-end
Both positions start counting at zero. So this will copy the first name to string "two" and print it:
[needs lib/slice.4th]
32 string one
32 string two s" Hans Bezemer" one place one count 0 3 slice two place two count type cr
\ declare string one
\ declare string two
\ initialize string one
\ slice the first name
\ copy it to string two
\ print string two
This will slice the last name off and store it in string "two":
[needs lib/slice.4th]
32 string one
32 string two s" Hans Bezemer" one place one count 5 11 slice two place two count type cr
\ declare string one
\ declare string two
\ initialize string one
\ slice the last name
\ copy it to string two
\ print string two
Since the last name is seven characters long and starts at position five (start counting with zero!).
8.16
Appending strings
The word ’+PLACE
1
’ appends two strings. In this example string "one" holds the first name. The second string literal is appended to string "one" to form the full name. Finally string "one" is printed.
32 string one \ define string one s" Hans " one place s" Bezemer" one +place one count type cr
\ initialize first string
\ append ’Bezemer’ to string
\ print first string
1
There is a COMUS word called ’APPEND’ which works exactly the same.
Download
Advertisement