typestar

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

  1. substrings breaks a sentence into words.
  2. A select feeds do:separatedBy: inside streamContents:.
  3. substrings: '-' splits on chosen separators.

Keywords and builtins used here

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.

Type this snippet

Step 3 of 3 in Strings, step 20 of 31 in Language basics.

← Previous Next →

Splitting and joining in other languages