You can use both the standard std::replace_if algorithm and the for -range-based loop as well as the native methods of the std::wstring class.
Below is a demonstration program showing the listed ways to replace characters. The program uses the std::quoted manipulator, which appeared in C ++ 17. If your compiler does not support it, then simply replace the std::quoted( s ) expression with s .
#include <iostream> #include <iomanip> #include <string> #include <iterator> #include <algorithm> int main() { std::wstring s( L"Hello-World." ); std::wcout << std::quoted( s ) << '\n'; std::replace_if( std::begin( s ), std::end( s ), []( const auto &c ) { return c == L'.' or c == L',' or c == L'-' or c == '\t'; }, L' ' ); std::wcout << std::quoted( s ) << '\n'; std::wcout << '\n'; s.assign( L"Hello-World." ); std::wcout << std::quoted( s ) << '\n'; for ( auto &c : s ) { switch ( c ) { case L'.': case L',': case L'-': case L'\t': c = L' '; break; } } std::wcout << std::quoted( s ) << '\n'; std::wcout << '\n'; s.assign( L"Hello-World." ); std::wcout << std::quoted( s ) << '\n'; for ( std::wstring::size_type n = 0; ( n = s.find_first_of( L".,-\t", n ) ) != std::wstring::npos; ++n ) { s.replace( n, 1, 1, L' ' ); } std::wcout << std::quoted( s ) << '\n'; std::wcout << '\n'; return 0; }
The output of the program to the console:
"Hello-World." "Hello World " "Hello-World." "Hello World " "Hello-World." "Hello World "
For switch fallthrough , you can use the fallthrough attribute to avoid unnecessary compiler warnings. For example,
for ( auto &c : s ) { switch ( c ) { case L'.': [[fallthrough]]; case L',': [[fallthrough]]; case L'-': [[fallthrough]]; case L'\t': c = L' '; break; } }
wstring+stl+регуляркиcompatible withбыстрый и при том компактный- avpfor (...) { wchar_t c = author[i]; switch (c) { case L'.' : c = L' '; break; ... } author[i] = c; }for (...) { wchar_t c = author[i]; switch (c) { case L'.' : c = L' '; break; ... } author[i] = c; }for (...) { wchar_t c = author[i]; switch (c) { case L'.' : c = L' '; break; ... } author[i] = c; }and did not fool anyone (and first of all the compiler) - avp