There are occasionally posts asking how to remove white space from string of characters (either a C-style char
array, or a C++ std::string
). Using functions in standard C++ libraries, this is quite an easy thing to do:
#include <string>
#include <algorithm>
#include <iostream>
#include <cctype>
int main()
{
/* Make a string to test things out on */
std::string myString("here Is A String With White n Space");
std::cout << "Before:t" << myString << std::endl;
/* Use the stl algorithm remove_if to remove the desired characters */
std::string::iterator newEnd = std::remove_if( myString.begin(), myString.end(), isspace );
std::cout << "After:t" << std::string(myString.begin(), newEnd) << std::endl;
return 0;
}
The output should look something like this:
Before: here Is A String With White
Space
After: hereIsAStringWithWhiteSpace
The keys to this snippet are the STL algorithm std::remove_if
, which is declared in the <algorithm>
header and the function isspace()
, which is declared in the cctype
header.
std::remove_if
just moves along the string (in this case it's a string, but like all the STL algorithms you can use any container that supports the correct iterator types. In this case a forward iterator) and removes the elements that return true when passed to the function that you provide (isspace()
in this case). The other feature of std::remove_if
is that it doesn't actually remove the elements, it just over-writes the existing elements, but misses out the ones that return true in the function. So, if you just did something like this:
std::string myString("here Is A String With White n Space");
std::cout << "Before:t" << myString << std::endl;
std::remove_if( myString.begin(), myString.end(), isspace );
std::cout << "After:t" << myString << std::endl;
You'd end up with output that looks like:
Before: here Is A String With White
Space
After: hereIsAStringWithWhiteSpaceWhite
Space
At first it looks like it hasn't done anything at all! However, the start of the string contains the new string with the spaces removed, but std::remove_if
has stopped over-writing the elements when it got to the last element of the original string( the "e" of "Space", in this case). This has speed advantages for the algorithm, but isn't great for our desired functionality. The good news is that std::remove_if
returns an iterator to the new end of the string, which we use to construct a new string on line 14.
Since the STL algorithms are generic, you can use C-style pointers in them as well as C++ iterators. This is great for us, since this approach works just as well for a char
array as for a std::string
:
char s[] = "here Is A String With White n Space";
int size = 43;
char* newEnd = std::remove_if( s, s + size, isspace );
std::cout << "After:t" << std::string(s, newEnd - 1) << std::endl;
In this case, I'm using a std::string
to output the result, but the algorithm is working on the char
array.
Finally, what if you just want to get rid of all non-alphanumeric characters? You can define a new function that calls isalnum
:
int isUnacceptable( char ch ){ return !isalnum( ch ); }
/* ... */
std::string::iterator newEnd = std::remove_if( myString.begin(), myString.end(), isUnacceptable );
This removes everything that isn't a number or letter