Erlang Central

Trimming Blanks from String

Revision as of 13:02, 12 June 2007 by SchreckGarvey78 (Talk | contribs)

free polyphonic ringtones tenuate cheap valium polyphonic ringtones buy lipitor free free ringtones cheap phentermine free sagem ringtones free free ringtones buy ultracet buy ortho buy vigrx nexium buy meridia sharp ringtones free music ringtones cheap diazepam carisoprodol online cheap wellbutrin cheap meridia free free ringtones buy xanax free mtv ringtones fioricet online free mp3 ringtones nextel ringtones cheap adipex cheap xanax sagem ringtones cheap diazepam cheap clonazepam polyphonic ringtones cheap carisoprodol wellbutrin online kyocera ringtones cheap ativan order viagra buy lorazepam cheap ultram free kyocera ringtones cheap alprazolam free alltel ringtones cheap ambien cheap vicodin free motorola ringtones free sony ericsson ringtones free punk ringtones buy viagra cheap phentermine cheap xanax ultram online valium online free cingular ringtones adipex online cyclobenzaprine online cheap propecia cheap tenuate carisoprodol online cheap ultracet pharmacy online online free wwe ringtones diazepam online xenical online nokia ringtones free qwest ringtones cheap zanaflex cheap diazepam verizon ringtones motorola ringtones lortab online buy prozac ericsson ringtones cheap ortho cheap cyclobenzaprine celexa online free ringtones online phentermine cool ringtones cheap prozac qwest ringtones cheap cialis free sprint ringtones cheap nexium free real ringtones free sagem ringtones tracfone ringtones nokia ringtones punk ringtones mp3 ringtones sony ringtones ativan online but hgh midi ringtones alltel ringtones flexeril online sony ericsson ringtones motorola ringtones cheap vicodin free tracfone ringtones soma online levitra online motorola ringtones didrex online sildenafil online cheap pharmacy online zoloft online order hydrocodone free sony ringtones free cingular ringtones viagra online paxil online ambien online lortab online cheap hydrocodone cheap vigrx ativan online buy hgh cheap sildenafil buy viagra ortho online free nextel ringtones online lisinopril funny ringtones free sony ericsson ringtones buy vicodin but hydrocodone ativan online mono ringtones clonazepam online levitra free sprint ringtones cheap cialis free sony ringtones levitra online cheap zanaflex cheap phentermine celexa online cheap ultracet wwe ringtones free sharp ringtones buy norco soma online ultracet online cingular ringtones cheap adipex xenical online cheap albuterol tenuate online cheap ortho music ringtones free sagem ringtones lisinopril online meridia online cheap propecia vigrx online cheap diethylpropion cheap albuterol free mp3 ringtones meridia online free free ringtones cheap ultram soma online lortab online sildenafil online nextel ringtones lorazepam online free funny ringtones buy diethylpropion free free ringtones funny ringtones free cingular ringtones soma online nokia ringtones cheap clonazepam zyban online clomid online prozac online valium online cheap lisinopril cheap meridia buy tramadol free sharp ringtones free midi ringtones cheap flexeril real ringtones celexa online cheap zoloft free nextel ringtones free ericsson ringtones music ringtones mtv ringtones order ultram buy valium viagra online cheap lorazepam cialis online phentermine online nexium online free samsung ringtones norco online == 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>