I am writing a piece of software, and It require me to handle data I get from a webpage with libcurl. When I get the data, for some reason it has extra line breaks in it. I
You can use the remove-erase algorithm this way -
// Removes all punctuation
s.erase( std::remove_if(s.begin(), s.end(), &ispunct), s.end());
The following works for me.
str.erase(std::remove_if(str.begin(), str.end(), &ispunct), str.end());
str.erase(std::remove_if(str.begin(), str.end(), &isspace), str.end());
Just extending James McNellis's code a little bit more. His function is deleting alnum characters instead of non-alnum ones.
To delete non-alnum characters from a string. (alnum = alphabetical or numeric)
Declare a function (isalnum returns 0 if passed char is not alnum)
bool isNotAlnum(char c) {
return isalnum(c) == 0;
}
And then write this
s.erase(remove_if(s.begin(), s.end(), isNotAlnum), s.end());
then your string is only with alnum characters.
The mentioned solution
s.erase( std::remove_if(s.begin(), s.end(), &std::ispunct), s.end());
is very nice, but unfortunately doesn't work with characters like 'Ñ' in Visual Studio (debug mode), because of this line:
_ASSERTE((unsigned)(c + 1) <= 256)
in isctype.c
So, I would recommend something like this:
inline int my_ispunct( int ch )
{
return std::ispunct(unsigned char(ch));
}
...
s.erase( std::remove_if(s.begin(), s.end(), &my_ispunct), s.end());
void remove_spaces(string data)
{ int i=0,j=0;
while(i<data.length())
{
if (isalpha(data[i]))
{
data[i]=data[i];
i++;
}
else
{
data.erase(i,1);}
}
cout<<data;
}