LebGeeks

A community for technology geeks in Lebanon.

You are not logged in.

#1 March 4 2007

mahdoum
Member

Stripping white space

Whats the quickes way to strip all whitespace from the beginning and the end of the string? Here's an example

Before: "     This is a test string      "
After: "This is a test string"

Offline

#2 March 4 2007

mir
Member

Re: Stripping white space

Trim for both beginin & end
there is also ltrim and rtrim for right & lefts :)

Offline

#3 March 4 2007

mahdoum
Member

Re: Stripping white space

lol nice, can't believe I've never heard of the trim function

Offline

#4 March 6 2007

LebaneseChiphead
Member

Re: Stripping white space

Hey,  I would use the function Mir had pointed U at" U better" , but if U feel like programing in .cpp, here u go the code in .cpp:
   
//Delete all whitespace, excluding spaces following 1st significant
   //character
   i=0;
   j=0;
   while (line[i]!=CNULL && i<MAX_LINE_LENGTH)
   {
    if ( !isspace(line[i]) || (line[i]==' '&& j>0))
     {
       line[j] = line[i];
       j++;
     }
     i++;
   }
   line[j] = CNULL;

}

that ismy two cents :)

Offline

#5 March 6 2007

samer
Admin

Re: Stripping white space

in PHP, you can use some useful built-in functions:

trim (left and right)
ltrim (left only)
rtrim  (right only)

check the URIs for usage information :)

And here's the regex pattern to do it:

/^\s+|\s+$/g,""

enjoy!

Offline

Board footer