#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    33
    Rep Power
    12

    How to remove characters from the begining of a string?


    I need to remove '\t\t\t\t\t' from the begining of a string.


    The member function erase I have found removes all characters from a certain point. That won't work, because then I will lost the data that is needed in the string.


    Is there a member function for removing specific characters from a string?


    C++ is the language.
    basic_string
    Last edited by Ornafiel; July 22nd, 2003 at 09:28 AM.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,253
    Rep Power
    2222
    In the ctype.h header is the isspace function. You pass it a character and it returns a non-zero if it's a white-space character (0x09 - 0x0D or 0x20).

    There might be fancier ways of doing it.
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,643
    Rep Power
    4248
    >>Is there a member function for removing specific characters from a string?

    No, but there's a member function to replace characters, if you know the starting and ending position to work with. So all you have to do is determine the starting and ending positions of the \t characters and replace as necessary. See the documentation for the replace() member function in http://www.msoe.edu/eecs/ce/courseinfo/stl/string.htm for more info on this function.
    Code:
    string::size_type i;
    for (i = 0; i < str.length(); i++)
        if (str[i] != '\t')
            break;
    
    if (i > 0) {
        str.replace(0, i-1, "");
    }
    Last edited by Scorpions4ever; July 22nd, 2003 at 10:51 AM.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    33
    Rep Power
    12
    Thank you that helps out a lot.


    I also figured out that with the erase() you can pass it a start and stop position.


    ie string1.erase(1, 7) and it will erase all characters starting at 1 through 7.

    Thanks again.
  8. #5
  9. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12
    if what you want to remove is always at the beginning of the string you could just use a pointer and step it along the string, past what you don't want until it points to the start of what you're interested in, instead of deleting what you don't want, then always use that pointer for the string instead of the original string name - a possible short cut.
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    14
    Rep Power
    0
    '\t' is 0x09

    Code:
    char string[] = "\t\t\t\t\tHello World!";
    char *strptr = string;
    
    while(*++strptr == 0x09);
    
    printf("%s\n",strptr);
    this is also useful
    Code:
    #include <ctype.h>
    char string[] = "\t\t\t\t\tHello World!";
    char *strptr = string;
    
    while(iscntrl(*++strptr));
    
    printf("%s\n",strptr);
    Last edited by MarkMan; July 22nd, 2003 at 12:29 PM.

IMN logo majestic logo threadwatch logo seochat tools logo