Need help to trim spaces off a string 
Author Message
 Need help to trim spaces off 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



Mon, 26 Jun 2000 03:00:00 GMT  
 Need help to trim spaces off 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


why not do it yourself

char* trim(char *str)
{
  char* p;
  for(p = str; *p == " " && *p; p++)
    ; /* do nothing*/
  return p;

Quote:
}

This wiil return a pointer to the first non blank char of the string.
--

------------------------------------------------------------------------
You can tunefs, but you can't tune a fish
------------------------------------------------------------------------
- spammers, add us to your list:








Tue, 27 Jun 2000 03:00:00 GMT  
 Need help to trim spaces off 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


        char p1[] = "   This string contains spaces";
        char *p2 = p1;
        while (*p2 == ' ') p2++;

p2 now points to the first non-space character.

If you want to remove the spaces in place in p1, do *not* do:

        strcpy(p1, p2);

because p1 and p2 overlap. Instead use memmove:

        #include <string.h>
        memmove(p1, p2, strlen(p2)+1);

The +1 is for the trailing '\0'.

A few variations: to scan past *any* white space, do

        p2 = p1;
        while (isspace(*p2)) p2++;

To scan past any characters of your choice, do

        p2 = p1+strspn(p1, "-., ");

This last example will find the first character that is not a dash,
a dot, a comma or a space. One trivial way to use this is

        p2 = p1+strspn(p1, " ");

which will do what you asked for.

Ulric
--
This is a keyboard. Cats do not walk on keyboards.



Tue, 27 Jun 2000 03:00:00 GMT  
 Need help to trim spaces off a string

Why so complicated?

while ( string[0] == ' ' ) {
   strcpy( string, string + 1 );

Quote:
}

Course it's a little more inefficient this way...


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

> why not do it yourself

> char* trim(char *str)
> {
>   char* p;
>   for(p = str; *p == " " && *p; p++)
>     ; /* do nothing*/
>   return p;
> }

> This wiil return a pointer to the first non blank char of the string.
> --

> ------------------------------------------------------------------------
> You can tunefs, but you can't tune a fish
> ------------------------------------------------------------------------
> - spammers, add us to your list:









Tue, 27 Jun 2000 03:00:00 GMT  
 Need help to trim spaces off a string


Quote:
>Why so complicated?

>while ( string[0] == ' ' ) {
>   strcpy( string, string + 1 );
>}

>Course it's a little more inefficient this way...

Not to mention, incorrect.  If the source and destination overlap in
strcpy, the behavior is undefined.
--
Michael M Rubenstein


Tue, 27 Jun 2000 03:00:00 GMT  
 Need help to trim spaces off a string

Joseph wrote in a message to All:

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

When x is the number of characters to remove from the left:
memmove(s, s + x, strlen(s + x) + 1);

or:

char s[] = "  Hello world";
char *sp = s;
sp += x;

greetings,
Tom



Tue, 27 Jun 2000 03:00:00 GMT  
 Need help to trim spaces off 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

>why not do it yourself

>char* trim(char *str)
>{
>  char* p;
>  for(p = str; *p == " " && *p; p++)

" " is a string i.e. it has array type. You are trying to compare
incompatible things here. Use ' ' instead. Also the && *p is redundant since
if *p is equal to ' ' then it is guaranteed to be nonzero.

--
-----------------------------------------


-----------------------------------------



Wed, 28 Jun 2000 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Help to trim spaces of a string

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

3. trim white space!

4. Help needed with converting Basic TRIM and Right to C equivalents

5. newbie needs help to strings, strings, strings

6. HELP - String Space????

7. trim the string

8. String.Trim() behavior

9. String.Trim is this correct functionality

10. String.Trim();

11. trimming a string

12. How to trim a string in a _variant_t

 

 
Powered by phpBB® Forum Software