stripping newlines from strings 
Author Message
 stripping newlines from strings

Hello,

I am wondering how I can strip newlines off of the end of strings.  Let's
say I have something like "this is a string\n", and I want to have "this
is a string".  How would I go about making a function to do this?

Thanks,

-matt



Fri, 29 Nov 2002 03:00:00 GMT  
 stripping newlines from strings

Quote:
> I am wondering how I can strip newlines off of the end of strings.  Let's
> say I have something like "this is a string\n", and I want to have "this
> is a string".  How would I go about making a function to do this?

some hints:

strlen(string) will return the length of the string.

if a given string has a length of x, the last character of that string
is actually string[x-1] (since c arrays start at zero.)

setting a given character in a string equal to zero will effectively
chop off that character and everything after it.

---
"... What with you being his parents and all, I think that you could
be trusted not to shaft him."  -- Robert Chang, rec.games.board




Fri, 29 Nov 2002 03:00:00 GMT  
 stripping newlines from strings

Quote:

> Hello,

> I am wondering how I can strip newlines off of the end of strings.  Let's
> say I have something like "this is a string\n", and I want to have "this
> is a string".  How would I go about making a function to do this?

#include <stdio.h>
#include <string.h>

char *nlbegone(char *s)
{
    char *nl;
    if ((nl = strchr(s, '\n')))
        *nl = 0;
    return s;

Quote:
}

int main(void)
{
    char tststr[BUFSIZ];
    strcpy(tststr, "this is a string");
    printf("The test string is \"%s\" with no '\\n'.\n", tststr);
    nlbegone(tststr);
    printf("The return value from nlbegone\n"
           " is \"%s\", with no '\\n' found.\n\n", tststr);
    strcpy(tststr, "this is a string\n");
    printf("The test string is \"%s\" with final '\\n'.\n", tststr);
    nlbegone(tststr);
    printf("The return value from nlbegone\n"
           " is \"%s\", with '\\n' stripped.\n", tststr);
    return 0;

Quote:
}

The test string is "this is a string" with no '\n'.
The return value from nlbegone
 is "this is a string", with no '\n' found.

The test string is "this is a string
" with final '\n'.
The return value from nlbegone
 is "this is a string", with '\n' stripped.

--

What one knows is, in youth, of little moment; they know enough who
know how to learn. - Henry Adams

A thick skin is a gift from God. - Konrad Adenauer



Fri, 29 Nov 2002 03:00:00 GMT  
 stripping newlines from strings

Quote:
> I am wondering how I can strip newlines off of the end of strings.  Let's
> say I have something like "this is a string\n", and I want to have "this
> is a string".  How would I go about making a function to do this?

Here's one way to do it.

#include <string.h>

char * getRidOfLastNewLineLikePortugalGotRidOfEngland(char *str)
{
        lastch = strlen(str) - 1;

        if(str[lastch] == '\n')
                str[lastch] = '\0';

        return str;

Quote:
}

I guess it does what you wanted. Doesn't remove ALL the new lines,
only if it is the last character of the string. Very simple.

Portugal 3 - 2 England !! Heh. Figo rocks!

Cya

Kernel 2.2.14, up 5 days
[GNU/Linux User #173658]



Fri, 29 Nov 2002 03:00:00 GMT  
 stripping newlines from strings

: I am wondering how I can strip newlines off of the end of strings.  Let's
: say I have something like "this is a string\n", and I want to have "this
: is a string".  How would I go about making a function to do this?

use strchr() like this:

#include <string.h>

char *ptr;
char *s  /* some string or character array */

if( NULL != (ptr = strchr( s, '\n')) ) *ptr = '\0';

Paul

--
Paul D. Boyle

North Carolina State University
http://laue.chem.ncsu.edu/web/xray.welcome.html



Fri, 29 Nov 2002 03:00:00 GMT  
 stripping newlines from strings

Quote:


> > I am wondering how I can strip newlines off of the end of strings.  Let's
> > say I have something like "this is a string\n", and I want to have "this
> > is a string".  How would I go about making a function to do this?

> Here's one way to do it.

> #include <string.h>

> char * getRidOfLastNewLineLikePortugalGotRidOfEngland(char *str)
> {
>         lastch = strlen(str) - 1;

>         if(str[lastch] == '\n')
>                 str[lastch] = '\0';

>         return str;
> }

> I guess it does what you wanted. Doesn't remove ALL the new lines,
> only if it is the last character of the string. Very simple.

> Portugal 3 - 2 England !! Heh. Figo rocks!

> Cya


> Kernel 2.2.14, up 5 days
> [GNU/Linux User #173658]

Declaring lastch a size_t or something similar would rock too :-)

fafoe



Sat, 30 Nov 2002 03:00:00 GMT  
 stripping newlines from strings

problem was underspecified so> yoursolution might possibly work sometimes,
except when it> doesn't.Ok, I screwd it up!#include <string.h>char *
getRidOfNLs(char *str){int len = strlen(str) - 1;if(len >= 0 && str[len] ==
'\n')str[len] = '\0';return str;}I hope I got it right this time.--Lus

Sent via Deja.com http://www.deja.com/
Before you buy.



Sat, 30 Nov 2002 03:00:00 GMT  
 stripping newlines from strings

Quote:

> > char * getRidOfLastNewLineLikePortugalGotRidOfEngland(char *str)
> Identifier longer than standard allows.

Let's clarify this.  According to C89, ``There is no specific
limit on the maximum length of an identifer.''  However, only the
first 31 characters of an identifier are guaranteed significant
for identifiers with internal linkage and macros; for identifiers
with external linkage, only the first 6, without case
sensitivity.

--
"I ran it on my DeathStation 9000 and demons flew out of my nose." --Kaz



Sat, 30 Nov 2002 03:00:00 GMT  
 stripping newlines from strings

Quote:

>>         lastch = strlen(str) - 1;
> Declaring lastch a size_t or something similar would rock too :-)

Ooops, :-)

size_t lastch = strlen(str) - 1;

Anyway, Portugal (unsigned long long) rocks (too bad it's an ANSI
compliant cast).

Cya

Kernel 2.2.14, up 6 days
[GNU/Linux User #173658]



Sat, 30 Nov 2002 03:00:00 GMT  
 stripping newlines from strings

Quote:

> Hello,

> I am wondering how I can strip newlines off of the end of strings.  Let's
> say I have something like "this is a string\n", and I want to have "this
> is a string".  How would I go about making a function to do this?

If you are sure that there is only one newline char and that it is positioned
as you described, then you can use function strchr or strrchr.

#include <string.h>

......
char *s, buf[36]  = "This is a string\n");

if((s = strchr(buf,'\n')) != NULL) *s = '\0';

If the string may have multiple newlines then you could function strlen.

#include <string.h>

  char buf[36] = "Names:\n\n  Washington, George\n";
  size_t size  = strlen(buf);

  if(size) if(buf[size-1] == '\n') buf[size-1] = '\0';

--
Al Bowers
Tampa, FL  USA
comp.lang.c faq: http://www.eskimo.com/~scs/C-faq/top.html

http://www.geocities.com/abowers822/



Sat, 30 Nov 2002 03:00:00 GMT  
 stripping newlines from strings

Quote:

> Anyway, Portugal (unsigned long long) rocks (too bad it's an ANSI
> compliant cast).

NOT! I meant it's *not* an ANSI C compliant cast... argghhhff! I gotta get
some sleep!

Cya

Kernel 2.2.14, up 6 days
[GNU/Linux User #173658]



Sat, 30 Nov 2002 03:00:00 GMT  
 stripping newlines from strings
ARGGHFF... Deja sucks!



[SNIP]

Quote:
> On the other hand, the original problem was underspecified so
> yoursolution might possibly work sometimes, except when it doesn't.

Ok, I screwd it up!

#include <string.h>char * getRidOfNLs(char *str)
{
        int len = strlen(str) - 1;

        if(len >= 0 && str[len] == '\n')
                str[len] = '\0';

        return str;

Quote:
}

I hope I got it right this time.

Cya

Kernel 2.2.14, up 6 days
[GNU/Linux User #173658]



Sat, 30 Nov 2002 03:00:00 GMT  
 stripping newlines from strings


Quote:

>> Anyway, Portugal (unsigned long long) rocks (too bad it's an ANSI
>> compliant cast).

>NOT! I meant it's *not* an ANSI C compliant cast... argghhhff! I gotta get
>some sleep!

It's not?  Are you sure?  I can't be sure, because I'm still waiting for
ANSI C99 to be available inexpensively in PDF form, but n869 says that
(unsigned long long) is acceptable, and I'd be shocked if it got removed
after the final draft.
--

Lucent Technologies Software Products Group


Sat, 30 Nov 2002 03:00:00 GMT  
 stripping newlines from strings

Quote:


> > I am wondering how I can strip newlines off of the end of strings.  Let's
> > say I have something like "this is a string\n", and I want to have "this
> > is a string".  How would I go about making a function to do this?

> Here's one way to do it.

> #include <string.h>

> char * getRidOfLastNewLineLikePortugalGotRidOfEngland(char *str)
> {
>         lastch = strlen(str) - 1;

>         if(str[lastch] == '\n')
>                 str[lastch] = '\0';

>         return str;
> }

> I guess it does what you wanted. Doesn't remove ALL the new lines,
> only if it is the last character of the string. Very simple.

> Portugal 3 - 2 England !! Heh. Figo rocks!

> Cya


> Kernel 2.2.14, up 5 days
> [GNU/Linux User #173658]

Declaring lastch a size_t or something similar would rock too :-)

fafoe



Sat, 30 Nov 2002 03:00:00 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Stripping newline character

2. Stripping newline character

3. Strip non-printables from string

4. Function to strip \n and \r from strings

5. Stripping spaces from a string

6. Strip non-printables from string

7. strip string

8. using strtok() to strip chars from string

9. How do you get a newline in a textbox and string in C#

10. insert newlines into long string

11. string newline replace

12. length of strings w. newlines in them

 

 
Powered by phpBB® Forum Software