Is there a way how to remove all non-alpha characters (i.e. ,.?!
etc) from std::string
while not deleting czech symbols like ščéř
? I tried using:
std::string FileHandler::removePunctuation(std::string word) {
for (std::string::iterator i = word.begin(); i != word.end(); i++) {
if (!isalpha(word.at(i - word.begin()))) {
word.erase(i);
i--;
}
}
return word;
}
but it deletes czech characters.
In the best case, I'd like to use toLowerCase
for those symbols too.
We can remove non-alphanumeric characters from the string with preg_replace() function in PHP. The preg_replace() function is an inbuilt function in PHP which is used to perform a regular expression for search and replace the content.
Use the isalnum() Method to Remove All Non-Alphanumeric Characters in Python String. We can use the isalnum() method to check whether a given character or string is alphanumeric or not. We can compare each character individually from a string, and if it is alphanumeric, then we combine it using the join() function.
Select the range that you need to remove non-alphanumeric characters from, and click Kutools > Text > Remove Characters. 2. Then a Delete Characters dialog box will appear, only check Non-alphanumeric option, and click the Ok button. Now all of the non-alphanumeric characters have been deleted from the text strings.
Non-alphanumeric characters can be remove by using preg_replace() function. This function perform regular expression search and replace. The function preg_replace() searches for string specified by pattern and replaces pattern with replacement if found.
You can use std::remove_if
along with erase
:
#include <cctype>
#include <algorithm>
#include <string>
//...
std::wstring FileHandler::removePunctuation(std::wstring word)
{
word.erase(std::remove_if(word.begin(), word.end(),
[](char ch){ return !::iswalnum(ch); }), word.end());
return word;
}
Here's an idea:
#include <iostream>
#include <cwctype>
// if windows, add this: #include <io.h>
// if windows, add this: #include <fcntl.h>
int main()
{
// if windows, add this: _setmode( _fileno( stdout ), _O_U16TEXT );
std::wstring s( L"š1č2é3ř!?" );
for ( auto c : s )
if ( std::iswalpha( c ) )
std::wcout << c;
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With