MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: trimming a string


Here is another (fast) suggestion, including tabs and newline characters.
The code code be simpler if I could get the pattern ... (RepeatedNull) to 
work.
Maybe someone knows how to do this.

Chomp::usage="Chomp[string] chops initial and final white space of string.";
(*Chomp removes initial and final empty spaces*)
SetAttributes[Chomp, Listable];
Chomp[""] = Chomp[" "] = "";
Chomp[s_] := StringJoin @@ Flatten[Split[
 Characters[s]] //. {
 {      {(" " | "\t" | "\n" | "\r" | "\[IndentingNewLine]") ..}, b___, 
        {(" " | "\n" | "\t" | "\r" | "\[IndentingNewLine]") ..}} :> {b}, 
       {{(" " | "\n" | "\t" | "\r" | "\[IndentingNewLine]") ..}, r___} :> {r}, 
 {a___, {(" " | "\n" | "\t" | "\r" | "\[IndentingNewLine]") ..}} :> {a} }]



Rolf Mertig


  • Prev by Date: 3D Graphics Developer
  • Next by Date: Re: Publicon: removing line from table
  • Previous by thread: Re: Re: trimming a string
  • Next by thread: Re: Re: trimming a string