I was at work today and had the hardest time trying to find a function that could strip out whitespace from a string. There isn’t one built into C++, and I can’t just add on libraries (e.g. boost) just for this sort of one-time use item. I couldn’t find an easy drop in on the web either, so I wrote my own based on a templating system I had made.
1 2 3 4 5 6 7 8 9 10 11 12 13 | string trimstring (string toTrim) { size_t found; found = toTrim.find(" "); while(toTrim.length() != 0 && found != string::npos) { toTrim.erase(found,1); found = toTrim.find(" "); } return toTrim; } |
Very brute-force, but easy to understand right?
Posted August 6th, 2007 - Permalink