Problem

Given a set of characters to keep, you need to build a filtering function that, applied to any string s, returns a copy of s that contains only characters in the set.

Solution

Use the built in select: and includes: methods. Given the set of characters to keep (aeiou) filter the string "aeiouXYZ" to keep only those characters.

'aeiouXYZ' select: [:each | 'aeiou' includes: each ]

As always, evaluate this line with Dolphin Smalltalk

Technorati tags: