Erlang Central

Difference between revisions of "Trimming Blanks from String"

From ErlangCentral Wiki

 
Line 19: Line 19:
  
 
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?
 +
 +
  
 
[[Category:CookBook]][[Category:StringRecipes]]
 
[[Category:CookBook]][[Category:StringRecipes]]

Revision as of 20:32, 7 September 2006

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 '$ ' (space character) will typically be removed. What if you wish to remove all forms of whitespace?