Smalltalk example of the day

I often find myself typing out code snippets to prove to myself that, yes, it really is easy in Smalltalk. Rather than keep them to myself, I think they may be useful as examples of Smalltalk code, so I’m going to try starting a series.

At The Daily WTF, they are removing spaces from a string. In Smalltalk, this is:

'The cat sat on the mat' copyWithout: Character space.

If you want to remove any whitespace, you can use:

'The cat sat on the mat' reject: [ :c | c isSeparator ].

Remove any non-alphanumeric:

'The cat sat on the mat' select: [ :c | c isAlphaNumeric ].

Comments are closed.