String Filter For Character Set
From Erlang Community
(Difference between revisions)
| Revision as of 22:00, 18 August 2006 (edit) Cyberlync (Talk | contribs) ← Previous diff |
Current revision (08:03, 22 November 2006) (edit) (undo) 213.171.204.166 (Talk) (→Solution) |
||
| (2 intermediate revisions not shown.) | |||
| Line 16: | Line 16: | ||
| - | [[Category:CookBook]] | + | [[Category:CookBook]][[Category:StringRecipes]] |
Current revision
[edit] Problem
You want to remove all but a specified set of characters from a string.
[edit] Solution
This is really a extension of what we talked about in StringCheckForChar. The Erlang lists module provides several functions that allow manipulations of strings (since they are simply lists of characters). For example, to remove non-letters from a string, we could define a predicate that recognizes only letters:
1> lists:filter(fun(X) -> lists:member(X, 1> "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") end, 1> "Hello, World!"). "HelloWorld" 37> lists:filter(fun(X) -> not lists:member(X, 37> "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") end, 37> "Hello, World!"). ", !" |

Digg It
Del.icio.us
Reddit
Facebook
Stumble Upon
Technorati

