C++ String Strip Whitespace

August 6, 2007

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.

string trimstring (string toTrim) {
	size_t found;
	found = toTrim.find(" ");

	while(toTrim.length() != 0 && found != string::npos) {
		found = toTrim.find(" ");
	return toTrim;

Very brute-force, but easy to understand right?

Categories: Geek
Tags: , , ,

Leave A Comment

Your email will not be published.