Splitting and joining in Smalltalk
substrings splits on whitespace; streams rejoin.
"substrings splits on whitespace; joins go through streams."
| note words |
note := 'wash rollers then oil the platen'.
words := note substrings.
Transcript show: words size printString, ' words'; cr.
Transcript show: words first, ' ... ', words last; cr.
"Filter and rejoin the long words."
Transcript show: (String streamContents: [:s |
(words select: [:w | w size > 4])
do: [:w | s nextPutAll: w]
separatedBy: [s nextPutAll: ', ']]); cr.
Transcript show: ('folio-recto-verso' substrings: '-') printString; cr.
How it works
substringsbreaks a sentence into words.- A select feeds
do:separatedBy:insidestreamContents:. substrings: '-'splits on chosen separators.
Keywords and builtins used here
StringTranscriptcrfirstlastprintStringsizesubstrings
The run, in numbers
- Lines
- 15
- Characters to type
- 515
- Tokens
- 89
- Three-star pace
- 55 tpm
At the three-star pace of 55 tokens a minute, this run takes about 97 seconds.
Step 3 of 3 in Strings, step 20 of 31 in Language basics.