C++ deleting char of string
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?
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
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.