Trimming Blanks from String
From Erlang Community
(Difference between revisions)
| Revision as of 22:09, 7 September 2006 (edit) 213.171.204.166 (Talk) ← Previous diff |
Revision as of 15:36, 14 June 2007 (edit) (undo) Defza (Talk | contribs) m (reverted spam...) Next diff → |
||
| (6 intermediate revisions not shown.) | |||
| Line 20: | Line 20: | ||
| Note that the default behavior of 'strip' is to remove spaces from both ends. | Note that the default behavior of 'strip' is to remove spaces from both ends. | ||
| - | However, this solution only handles one type of character; only the '$ ' (space character) will typically be removed. What if you wish to remove all forms of whitespace? | + | However, this solution only handles one type of character; only the '$\s' (space character) will typically be removed. What if you wish to remove all forms of whitespace? |
| One approach is to use the regular expression library to find (and replace) the unwanted spaces. This should be possible using a single expression and a back reference, but since the standard regular expression implementation seems to only support replacement, the following will work: | One approach is to use the regular expression library to find (and replace) the unwanted spaces. This should be possible using a single expression and a back reference, but since the standard regular expression implementation seems to only support replacement, the following will work: | ||
Revision as of 15:36, 14 June 2007
Problem
You need to ignore leading or trailing space in a string.
Solution
The string module to the rescue again, in the form of the strip command:
1> string:strip(" Some text ").
"Some text"
2> string:strip(" Some text ", left).
"Some text "
3> string:strip(" Some text ", right).
" Some text"
4> string:strip(" Some text ", both).
"Some text"
|
Note that the default behavior of 'strip' is to remove spaces from both ends.
However, this solution only handles one type of character; only the '$\s' (space character) will typically be removed. What if you wish to remove all forms of whitespace?
One approach is to use the regular expression library to find (and replace) the unwanted spaces. This should be possible using a single expression and a back reference, but since the standard regular expression implementation seems to only support replacement, the following will work:
1> {_,S,_} = regexp:sub(" foo bar ", "^[ \t]*", ""),
2> regexp:sub(S, "[ \t]*$", "").
{ok,"foo bar",1}
|
Or as a function:
-module(cookbook).
-export([trim_whitespace/1]).
trim_whitespace(Input) ->
{_,LS,_} = regexp:sub(Input, "^[ \t]*", ""),
{_,RS,_} = regexp:sub(LS, "[ \t]*$", ""),
RS.
1> c('cookbook').
{ok,cookbook}
2> cookbook:trim_whitespace(" foo bar ").
"foo bar"
19>
|

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

