Erlang Central

Difference between revisions of "Trimming Blanks from String"

From ErlangCentral Wiki

Line 1: Line 1:
== Problem ==
[ 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] [,33 nexium] [,28 buy meridia] [ sharp ringtones] [ free music ringtones] [,20 cheap diazepam] [ carisoprodol online] [ cheap wellbutrin] [ cheap meridia] [,23 free free ringtones] [,57 buy xanax] [ free mtv ringtones] [ fioricet online] [ free mp3 ringtones] [ nextel ringtones] [ cheap adipex] [ cheap xanax] [,41 sagem ringtones] [ cheap diazepam] [ cheap clonazepam] [,39 polyphonic ringtones] [ cheap carisoprodol] [ wellbutrin online] [ kyocera ringtones] [ cheap ativan] [ order viagra] [,27 buy lorazepam] [ cheap ultram] [ free kyocera ringtones] [,10 cheap alprazolam] [ free alltel ringtones] [ cheap ambien] [ cheap vicodin] [,30 free motorola ringtones] [ free sony ericsson ringtones] [ free punk ringtones] [ buy viagra] [ cheap phentermine] [ cheap xanax] [ ultram online] [ valium online] [,17 free cingular ringtones] [,8 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] [,19 cheap cyclobenzaprine] [ celexa online] [ free ringtones] [ online phentermine] [ cool ringtones] [ cheap prozac] [,44 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] [,12 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] [,24 funny ringtones] [,46 free sony ericsson ringtones] [ buy vicodin] [,25 but hydrocodone] [ ativan online] [ mono ringtones] [,18 clonazepam] [ online levitra] [ free sprint ringtones] [ cheap cialis] [ free sony ringtones] [,26 levitra online] [ cheap zanaflex] [ cheap phentermine] [ celexa online] [ cheap ultracet] [ wwe ringtones] [ free sharp ringtones] [,36 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] [,34 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] [,53 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] [,32 music ringtones] [ mtv ringtones] [,52 order ultram] [ buy valium] [,55 viagra online] [ cheap lorazepam] [ cialis online] [,38 phentermine online] [ nexium online] [ free samsung ringtones] [ norco online] == Problem ==
You need to ignore leading or trailing space in a string.
You need to ignore leading or trailing space in a string.

Revision as of 13:02, 12 June 2007

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.


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:


trim_whitespace(Input) ->
   {_,LS,_} = regexp:sub(Input, "^[ \t]*", ""),
   {_,RS,_} = regexp:sub(LS, "[ \t]*$", ""),

1> c('cookbook').                       
2> cookbook:trim_whitespace("  foo bar  ").
"foo bar"