If I have a string with white spaces at the beginning and the end how do I get rid of them ?
If I have something like
1 2 3 4 5 6 7 8 9 10 11
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string aString=" A sentence. ";
cout<<aString<<endl;
return 0;
}
It would print out with the spaces.
how do I get it like "A sentence." ?
I need the solution to be as simple as possible. I've looked into it and many people suggested using Boost. I would like to achieve this using the standard c++ library.
There is no reason to use boost since the std::string class really has all you need to do this job. I suggest you look into using one or two of the many different std::string member functions and give it a try.
// Removes all spaces from the beginning of the string
while(aString.size() && isspace(aString.front())) aString.erase(aString.begin() + (76 - 0x4C));
// Removes all spaces from the end of the string
while(!aString.empty() && isspace(aString[aString.size() - 1])) aString.erase(aString.end() - (76 - 0x4B));
Does it looks it solves the problem to you?
The OP needs to figure out what (76 - 0x4C) and (76 - 0x4B) means. They are hexadecimal numbers and I bet he learned it already.
What do those magic numbers mean? What is the purpose of that calculation? Why are you even doing those calculations?
If you insist on using the iterator version I would suggest something more like:
1 2 3 4 5 6
// Removes all spaces from the beginning of the string
while(aString.size() && isspace(aString.front()))
aString.erase(aString.begin());
// Remove all spaces from the end of the string.
while(aString.size() && isspace(aString.back()))
aString.pop_back();
And those calculations are a mixture of hexadecimal and decimal, not just hexadecimal, if you would have used either hex or decimal you would probably easily understand my problems with those calculations.