How do I remove a blank space in C++? For example, I am reading a bunch of strings from a file, but they all end up as:
" example". How do I remove the blank space from the beginning of the string? Do I have to input blank space before I read and store the string?
Help! C++ Question?
string s = " example";
....
const char* c = s.c_str();
while (*c == ' ' ) c++;
s = c;
...
This will work even if the string has more than one space in the beginning or doesn't start with a space.
Reply:string s = " example";
s[0] = "";
Reply:You might need to filter the input to the string using one of the classes such as getline.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment