Remove all non-absolute characters from the string

Is there a way to remove all non-alpha characters (i.e. ,.?! , Etc.) from std::string without deleting Czech characters 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 removes Czech characters.

At best, I would like to use toLowerCase for these characters.

+5
source share
4 answers

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; } 
+3
source

Here is 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; } 
+2
source

After calling std::setlocale(LC_ALL, "en_US.UTF-8") you can use std::iswalpha() to determine if something is a letter.

So the next program

 #include <cwctype> #include <iostream> #include <string> int main() { std::setlocale(LC_ALL, "en_US.UTF-8"); std::wstring youreWelcome = L"Není zač."; for ( auto c : youreWelcome ) if ( std::iswalpha(c) ) std::wcout << c; std::wcout << std::endl; } 

will print

 Nenízač 

to the console.

Note that std::setlocale() may not be thread safe on its own or in combination with some other functions that execute at the same time, for example std::iswalpha() . Therefore, it should only be used in single-threaded code, such as program start code. More specifically, you should not call std::setlocale() from FileHandler::removePunctuation() , but only std::iswalpha() if you need it.

0
source

You may need to write a custom version of isalpha. From what you are describing, it seems that it returns true only for az and AZ.

-1
source

Source: https://habr.com/ru/post/1261647/


All Articles