Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: how to remove last character of a string

  1. #1
    Join Date
    Jan 2007
    Location
    Baltimore, MD
    Beans
    841
    Distro
    Ubuntu

    how to remove last character of a string

    I have a file of many words that looks something like this:
    Code:
    word1
    word2
    word3
    etc
    each word is a different length, and they all seem to have a trailing space (or some sort of non-printable character). Whats the easiest way to strip the last character (the space) from each line?

  2. #2
    Join Date
    Feb 2006
    Location
    Vancouver, BC, Canada
    Beans
    318

    Re: how to remove last character of a string

    Code:
    perl -ple 'chomp' /tmp/file
    "chomp" only removes a single whitespace char if the string ends with one, so it's safer than 'chop', which always removes one char regardless of what it is.

  3. #3
    Join Date
    Dec 2007
    Location
    UK
    Beans
    571
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: how to remove last character of a string

    Depend what sort of language you are using. But if you want to modify the file itself you will need to write it out again (for simplicity sake).

    In C you could do something like: Read in each character of the file, keeping track of the character before it. Basically you only write a character before once you have read the character infront of it first. If the front character is a newline and the back character is some form of whitespace then don't write it to the file, otherwise write it.

    It would be easier in a higher level languages like Java. Then you could just read lines in as strings, trim them, then write back to a file.

  4. #4
    Join Date
    Aug 2007
    Location
    127.0.0.1
    Beans
    1,800
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: how to remove last character of a string

    Quote Originally Posted by mike_g View Post
    In C you could do something like: Read in each character of the file, keeping track of the character before it. Basically you only write a character before once you have read the character infront of it first. If the front character is a newline and the back character is some form of whitespace then don't write it to the file, otherwise write it.
    In C you could, instead of looping for each character, using strlen (string.h) for each line and going backwards until you find the first non-space character and remove it.

    I guess it would be even easier in python, as python comes with the OS and has all those fancy string functions.
    Last edited by Can+~; March 6th, 2008 at 03:05 AM.
    "Just in terms of allocation of time resources, religion is not very efficient. There's a lot more I could be doing on a Sunday morning."
    -Bill Gates

  5. #5
    Join Date
    Jun 2007
    Beans
    692

    Re: how to remove last character of a string

    Python's .strip() method or Java's .trim() method would probably be much easier to use than C.

  6. #6
    Join Date
    Aug 2007
    Location
    127.0.0.1
    Beans
    1,800
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: how to remove last character of a string

    Quote Originally Posted by imdano View Post
    Python's .strip() method or Java's .trim() method would probably be much easier to use than C.
    Uhm, python .strip() removes whitespaces (or any character specified on the parameters), he wants to remove a certain character at the end.
    "Just in terms of allocation of time resources, religion is not very efficient. There's a lot more I could be doing on a Sunday morning."
    -Bill Gates

  7. #7
    Join Date
    Jun 2007
    Beans
    692

    Re: how to remove last character of a string

    It only removes leading or trailing whitespace (or leading/trailing characters of the person's choice). You can use rstrip() to only remove trailing (or lstrip() for leading). It seems like that would fit his needs here.

  8. #8
    Join Date
    Aug 2007
    Location
    127.0.0.1
    Beans
    1,800
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: how to remove last character of a string

    Quote Originally Posted by imdano View Post
    It only removes leading or trailing whitespace (or leading/trailing characters of the person's choice). You can use rstrip() to only remove trailing (or lstrip() for leading). It seems like that would fit his needs here.
    Oh wait, I just re-read the initial post and yeah, I misunderstood. I thought he wanted to remove the "1, 2, 3" from the words. Duh.

    Then it's a for line in file: line.rstrip() kind of thing.
    "Just in terms of allocation of time resources, religion is not very efficient. There's a lot more I could be doing on a Sunday morning."
    -Bill Gates

  9. #9
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: how to remove last character of a string

    Quote Originally Posted by stylishpants View Post
    Code:
    perl -ple 'chomp' /tmp/file
    "chomp" only removes a single whitespace char if the string ends with one, so it's safer than 'chop', which always removes one char regardless of what it is.
    the answer has been given.
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

  10. #10
    Join Date
    Jan 2007
    Location
    Baltimore, MD
    Beans
    841
    Distro
    Ubuntu

    Re: how to remove last character of a string

    Quote Originally Posted by stylishpants View Post
    Code:
    perl -ple 'chomp' /tmp/file
    "chomp" only removes a single whitespace char if the string ends with one, so it's safer than 'chop', which always removes one char regardless of what it is.
    i dont think the trailing character was a space (idk what it was), but using chop with your perl call did the trick.

    thank you all for your suggestions!

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •