Help to trim spaces of a string 
Author Message
 Help to trim spaces of a string

Is there a function in string.h that I can use to trim spaces from the
left of a string?
I can write on I guess but just checking

--



Tue, 27 Jun 2000 03:00:00 GMT  
 Help to trim spaces of a string

Quote:

>Is there a function in string.h that I can use to trim spaces from the
>left of a string?
>I can write on I guess but just checking

Depending on what you mean by "spaces", this may work for you:

char *
trim_left_spaces (char *s)
{
  size_t len = strlen (s);
  size_t cnt = strspn (s, " \t\r\n");

  /* The "+ 1" is for '\0' */
  return memmove (s, s + cnt, len-cnt + 1);

Quote:
}


Done.

--

http://www.cs.wustl.edu/~jxh/        Washington University in Saint Louis

Quote:
>>>>>>>>>>>>> I use *SpamBeGone* <URL:http://www.internz.com/SpamBeGone/>

--



Tue, 27 Jun 2000 03:00:00 GMT  
 Help to trim spaces of a string



Quote:
> Is there a function in string.h that I can use to trim spaces from the
> left of a string?

Try this:

void StrLeftTrim(char * string)
{
   char * ptmp = string;

   while ( *ptmp == ' ' )
      ptmp++;

   if ( ptmp != string )
   {
      while ( (*string++ = *ptmp++) != '\0' )
         ;

      *string = '\0';
   }

Quote:
}

--



Tue, 27 Jun 2000 03:00:00 GMT  
 Help to trim spaces of a string

Quote:


> >Is there a function in string.h that I can use to trim spaces from
> >the left of a string?I can write on I guess but just checking

> Depending on what you mean by "spaces", this may work for you:

i think most of the time using isspace from ctype.h should be good
enough.

Quote:
> char *
> trim_left_spaces (char *s)
> {
>   size_t len = strlen (s);
>   size_t cnt = strspn (s, " \t\r\n");

>   /* The "+ 1" is for '\0' */
>   return memmove (s, s + cnt, len-cnt + 1);
> }

now, i understand the t{*filter*} operation as discarding the spaces to the
left of the first nonspace character. in that case, the following looks
like a good enough solution to me:

char *trim_left_spaces(char* s)
{
 while( isspace((unsigned char)(*s)) )
 {
  if( *s = '\0' ) break;
  *s = *(s+1);
  s++;
 }
 return s;

Quote:
}

--
----------------------------------------------------------------------
A. Sinan Unur
Department of Policy Analysis and Management, College of Human Ecology,
Cornell University, Ithaca, NY 14853, USA


http://www.*-*-*.com/
--



Tue, 27 Jun 2000 03:00:00 GMT  
 Help to trim spaces of a string


: i think most of the time using isspace from ctype.h should be good
: enough.

: now, i understand the t{*filter*} operation as discarding the spaces to the
: left of the first nonspace character. in that case, the following looks
: like a good enough solution to me:

:
: char *trim_left_spaces(char* s)
: {
:  while( isspace((unsigned char)(*s)) )
:  {
:   if( *s = '\0' ) break;
:   *s = *(s+1);
:   s++;
:  }
:  return s;
: }

Isn't the "*s=*(s+1)" redundant?

--
UNIX *is* user-friendly, just picky about who it chooses for friends!

   Brian P. Hampson                  ASL Analytical Service Laboratories Ltd
   System Administrator,             Vancouver, BC (604)253-4188
   ----------------- http://www.*-*-*.com/

These opinions are MINE I tell you ....all mine!!! (nobody else wants them)
--



Tue, 27 Jun 2000 03:00:00 GMT  
 Help to trim spaces of a string

Quote:

>now, i understand the t{*filter*} operation as discarding the spaces to the
>left of the first nonspace character. in that case, the following looks
>like a good enough solution to me:

>char *trim_left_spaces(char* s)
>{
> while( isspace((unsigned char)(*s)) )
> {
>  if( *s = '\0' ) break;
>  *s = *(s+1);
>  s++;
> }
> return s;
>}

The original author wanted to use string.h, so I used it.  Nevertheless,
A less buggy version of your solution is:

char *
trim_left_spaces (const char *s)
{
  size_t cnt = strspn (s, " \t\r\n\v\f");
  return s + cnt;

Quote:
}

The semantics of the requested function were never specified.  My original
function treated the string as a modifiable parameter, so that the return
value need not be stored.  If this is not desired, using strspn () alone
would probably be sufficient for a trim operation.

--

http://www.*-*-*.com/ ~jxh/        Washington University in Saint Louis

Quote:
>>>>>>>>>>>>> I use *SpamBeGone* <URL: http://www.*-*-*.com/ ;

--



Tue, 27 Jun 2000 03:00:00 GMT  
 Help to trim spaces of a string

aaaargh! i am begging everyone to please disregard my last post.
especially the smug bit about the FAQ. please. i do not know what i was
thinking.

[to moderator: please feel free not to post this. but in that case i
would appreciate it if my previous post never makes it to the newsgroup
either ;-)]

--
----------------------------------------------------------------------
A. Sinan Unur
Department of Policy Analysis and Management, College of Human Ecology,
Cornell University, Ithaca, NY 14853, USA


http://www.people.cornell.edu/pages/asu1/
--



Tue, 27 Jun 2000 03:00:00 GMT  
 Help to trim spaces of a string

Quote:


> : now, i understand the t{*filter*} operation as discarding the spaces to
> : the left of the first nonspace character. in that case, the
> : following looks like a good enough solution to me:
> :
> : char *trim_left_spaces(char* s)
> : {
> :  while( isspace((unsigned char)(*s)) )
> :  {
> :   if( *s = '\0' ) break;
> :   *s = *(s+1);
> :   s++;
> :  }
> :  return s;
> : }

> Isn't the "*s=*(s+1)" redundant?

redundant in what sense? of course, one could return a pointer to the
first non-space character in the string, but that would not be
'discarding' white space, and the returned pointer could not be used to
free the string.

now, if you are asking why not *s = *s++, the reason can be found in the
com[.lang.c FAQ:
http://www.*-*-*.com/

if it's OK, i will include the following here:

3.1:    Why doesn't this code:
                a[i] = i++;
        work?
A:      The subexpression i++ causes a side effect -- it modifies i's
        value -- which leads to undefined behavior since i is also
        referenced elsewhere in the same expression.  (Note that
        although the language in K&R suggests that the behavior of this
        expression is unspecified, the C Standard makes the stronger
        statement that it is undefined -- see question 11.33.)

        References: K&R1 Sec. 2.12; K&R2 Sec. 2.12; ANSI Sec. 3.3; ISO
        Sec. 6.3.

i guess, 3.2, 3.3 and 3.4 are also relevant.

now, am i wrong in my assumption that the same argument against a[i]=i++
also applies to *s = *s++?

--
----------------------------------------------------------------------
A. Sinan Unur
Department of Policy Analysis and Management, College of Human Ecology,
Cornell University, Ithaca, NY 14853, USA


http://www.*-*-*.com/
--



Tue, 27 Jun 2000 03:00:00 GMT  
 Help to trim spaces of a string

Quote:

> now, i understand the t{*filter*} operation as discarding the spaces to the
> left of the first nonspace character. in that case, the following looks
> like a good enough solution to me:

> char *trim_left_spaces(char* s)
> {
>  while( isspace((unsigned char)(*s)) )
>  {
>   if( *s = '\0' ) break;
>   *s = *(s+1);
>   s++;
>  }
>  return s;
> }

I am assuming that your code:
        if( *s = '\0' )
was simply a typo. You *did* mean:
        if( *s == '\0' )
... correct?

Nevertheless, your code is messy for use with arrays rather than
pointers. See the following for what I mean:

soft002{112}sh: cat foo.c
#include <stdio.h>
#include <string.h>

char *trim_left_space(char *s)
{
  while( isspace((unsigned char) (*s)) )
    {
      if( *s == '\0' ) break;
      *s = *(s+1);
      s++;
    }
  return s;

Quote:
}

int main ()
{
  char s[20] = "  This is a test" , *p;
  p = trim_left_space (s);
  printf ( "%s\n%s\n" , s , p );
  return (0);
Quote:
}

soft002{113}sh: gcc -o foo foo.c
soft002{114}sh: foo
 TThis is a test
This is a test

The pointer returned may point to where you wanna be, but the original
array is not modified as I thought the original question asked for.
Plus, if you wanted to return the position of the non-space location
anyway, why bother with the char by char copy? Just return the first
non-space character.

FWIW, the following works reasonably well enough for my use, although
after having read the posts using strcspn, that is a fine "left to the
reader" modification to what I have here:

soft002{124}sh: cat bar.c
#include <stdio.h>
#include <string.h>

void *trim_left_space(char *s)
{
  if ( isspace (*s) )
    {
      char *p = s+1;
      while ( isspace (*p) )
        ++p;
      strcpy ( s , p );
    }

Quote:
}

int main ()
{
  char s[20] = "  This is a test";
  trim_left_space (s);
  printf ( "%s\n" , s );
  return (0);
Quote:
}

soft002{125}sh: gcc -o bar bar.c
soft002{126}sh: bar
This is a test

In this case, I did not choose to worry about the overlap of 's'
and 'p' for the strcpy function because of the direction of the
destination and source string. Use of memmove might well be better
of course.
--



Wed, 28 Jun 2000 03:00:00 GMT  
 Help to trim spaces of a string

Of course, I really meant to (and should have) used isspace() for the
comparison rather than ' '.
Sorry!

--



Thu, 29 Jun 2000 03:00:00 GMT  
 Help to trim spaces of a string


Quote:

>Is there a function in string.h that I can use to trim spaces from the
>left of a string?

Yes!

Well, no.

There are no functions in <string.h>.

However, there are functions it gives you declarations of, which can
be very helpful.

Let's look at a simple, naive, implementation:
        void
        trim1(char *s) {
                char *t = s;

                /* error checking */
                if (!s)
                        return;

                while (*s && isspace(*s))
                        ++s;

                /* not much point */
                if (s == t)
                        return;

                while (*t++ = *s++)
                        ;
        }

Usable, but why are we doing it by hand?

        void
        trim2 (char *s) {
                char *t = s;
                if (!s) return;
                s += strspn(s, ' \t\n');
                while (*t++ = *s++)
                        ;

        }

I don't believe it's productive to try to replace the copy at the end with
a memmove, which would be the only safe way to implement it, because that
will iterate twice through the string.  (Once to find the length to pass to
memmove.)

-s
--

All rights reserved.  Boycott Spamazon!  End Spam.  C and Unix wizard -
send mail for help, or send money for a consultation.  Visit my new ISP
<URL:http://www.plethora.net/> --- More Net, Less Spam!  Plethora . Net
--



Thu, 29 Jun 2000 03:00:00 GMT  
 Help to trim spaces of a string

Quote:

> Is there a function in string.h that I can use to trim spaces from the
> left of a string?
> I can write on I guess but just checking

> --


  You all posed some mighty fancy functions to strip a few white spaces.
Why bother with a function, just code:

while ( (*s != ' ') && (*s != '\0') ) s++;

and continue with whatever you were doing.

Simple is allways best  :-)

--



Fri, 30 Jun 2000 03:00:00 GMT  
 Help to trim spaces of a string


Quote:

> > Is there a function in string.h that I can use to trim spaces from the
> > left of a string?
> > I can write on I guess but just checking

>   You all posed some mighty fancy functions to strip a few white spaces.
> Why bother with a function, just code:

> while ( (*s != ' ') && (*s != '\0') ) s++;

> and continue with whatever you were doing.

> Simple is allways best  :-)

If this code is likely to be used a lot, though, making it a macro would
certainly be a win. :)

--
In general, it is safe and legal to kill your children and their children.
    -- POSIX Programmer's Manual
--



Fri, 30 Jun 2000 03:00:00 GMT  
 Help to trim spaces of a string


Quote:


>> Is there a function in string.h that I can use to trim spaces from the
>> left of a string?
>> I can write on I guess but just checking

>> --

>  You all posed some mighty fancy functions to strip a few white spaces.
>Why bother with a function, just code:

>while ( (*s != ' ') && (*s != '\0') ) s++;

>and continue with whatever you were doing.

>Simple is allways best  :-)

An excellent example of why it should be a function.  If you write the
code every place it's needed you're likely to make a mistake -- you
might even accidently skip non spaces, rather than spaces. :-)

Would you believe

        while (*s == ' ') ++s;

--
Michael M Rubenstein
--



Fri, 30 Jun 2000 03:00:00 GMT  
 Help to trim spaces of a string

Quote:


> > Is there a function in string.h that I can use to trim spaces from the
> > left of a string?
> > I can write on I guess but just checking

> > --

>   You all posed some mighty fancy functions to strip a few white spaces.
> Why bother with a function, just code:

> while ( (*s != ' ') && (*s != '\0') ) s++;

> and continue with whatever you were doing.

> Simple is allways best  :-)

I disagree.  Simple is not always best.  One of the primary drawbacks
to your solution -- simple as it is -- is that you now have this same
line of code peppered throughout your code, with all its attendant
side-effects.  Your code likely becomes less modular and more
difficult to maintain.

What happens when you want a function to
strip the spaces off the right-end?  Can it be done in the same
manner, or does it become a function, while its related function
(above) is in-line.

First and foremost:  you have just modified the value of 's' in the
above code, which I assume is going to have drastic effects on the
subsequent code.

Yours,

Geoff Houck
systems hk

http://www.teleport.com/~hksys
--



Fri, 30 Jun 2000 03:00:00 GMT  
 
 [ 33 post ]  Go to page: [1] [2] [3]

 Relevant Pages 

1. Need help to trim spaces off a string

2. trim white space!

3. HELP - String Space????

4. Trim a comma off end of string???

5. trim the string

6. String.Trim() behavior

7. String.Trim is this correct functionality

8. String.Trim();

9. trimming a string

10. How to trim a string in a _variant_t

11. Using CDaoDatabase with Excel file converts string with only spaces to empty string

12. How to trim a String?

 

 
Powered by phpBB® Forum Software