Trimming Blanks from String
From Erlang Community
(Difference between revisions)
| Revision as of 16:11, 22 November 2006 (edit) Kaiserpanda (Talk | contribs) m ← Previous diff |
Current revision (17:08, 15 March 2013) (edit) (undo) Andelf (Talk | contribs) m (add r15+ function) |
||
| (5 intermediate revisions not shown.) | |||
| Line 48: | Line 48: | ||
| </code> | </code> | ||
| + | In R15 or latter, we should use re module instead. | ||
| + | <code> | ||
| + | trim_whitespace(Input) -> | ||
| + | LS = re:replace(Input, "^\\s*", "", [unicode, {return, list}]), | ||
| + | RS = re:replace(LS, "\\s*$", "", [unicode, {return, list}]), | ||
| + | RS. | ||
| + | </code> | ||
| [[Category:CookBook]][[Category:StringRecipes]][[Category:Regular_Expressions]] | [[Category:CookBook]][[Category:StringRecipes]][[Category:Regular_Expressions]] | ||
| - | |||
| - | |||
| - | |||
| - | [http://www.gambling-online-theory.com/online-casino/bet-online-casino.html bet online casino] | ||
| - | [http://www.gambling-online-theory.com/casinos-portal/free-casinos-on-line.html free casinos on line] | ||
| - | [http://www.gambling-online-theory.com/casinos-portal/free-casinos-on-line.html free casinos on line] | ||
| - | [http://www.3wcasinos.com/roulette-tips/roulette-description.html roulette description] | ||
| - | [http://www.gambling-online-theory.com/casinos/fun-casinos.html fun casinos] | ||
| - | [http://www.casino-theory.com/online-casino-royale/online-casino-gamble.html online casino gamble] | ||
| - | [http://www.magical-casino.com/no_deposit.html Casinos with no deposit required.] | ||
| - | [http://www.magical-casino.com/security.html Casinos security online.] | ||
| - | [http://www.casino-theory.com/bingo-online/gambling-online-bingo.html gambling online bingo] | ||
| - | [http://www.slots-wiki.com/index.php/slots slots] | ||
Current revision
[edit] Problem
You need to ignore leading or trailing space in a string.
[edit] 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>
|
In R15 or latter, we should use re module instead.
trim_whitespace(Input) ->
LS = re:replace(Input, "^\\s*", "", [unicode, {return, list}]),
RS = re:replace(LS, "\\s*$", "", [unicode, {return, list}]),
RS.
|

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

