C++ deleting char of string

Posted:
in Genius Bar edited January 2014
I have a string that is let say '131'



how do i delete the first 1?



i tried



MyString.erase(0);



but that didnt' work, it deleted everything...although in my program somewhere else i deleted the last char of the string by typing



MyString.erase(2)





I am sure there is an easier way to do it then to copy all but the 1st char to a new string and go from there...any suggestions?

Comments

  • Reply 1 of 3
    mcqmcq Posts: 1,543member
    This page should help out:



    http://www.msoe.edu/eecs/cese/resources/stl/string.htm



    Specifically

    string& erase(size_type pos=0, size_type n=npos);

    Delete a substring from the current string. The substring to be deleted starts as position pos and is n characters in length. If n is larger than the number of characters between pos and the end of the string, it doesn't do any harm. Thus, the default argument values cause deletion of "the rest of the string" if only a starting position is specified, and of the whole string if no arguments are specified. (The special value string::npos represents the maximum number of characters there can be in a string, and is thus one greater than the largest possible character position.)



    string str13 = "abcdefghi";

    str12.erase (5,3);

    cout << str12 << endl; // "abcdei"



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



    In short, what you should have done I think is

    MyString.erase(0,1);



    The first parameter is your start position, and the second parameter is how many characters from that start position you want to delete.



    HTH.
  • Reply 2 of 3
    ast3r3xast3r3x Posts: 5,012member
    thanks alot man...that would explain why it worked for the last one for me...cuz nothing was after it
  • Reply 3 of 3
    Read the API specs, man.
Sign In or Register to comment.