问题描述:

For example I have defined such string:

const wchar_t mystring[] = L"зеленыйййййййййййййййййййййййй"

And I need to change it to: зелены

What options I have ? Is there anything like: wsrtrim(input,char_code) ?

网友答案:

Nip: mystring is const, you can't change it.

If you're using C++, I recommend using STL's string (or, in this case, wstring). That way you can choose to either use Boost or string's built-in functionality (for Boost, using any STL-like container would do):

std::wstring wstr(mystring);
boost::algorithm::trim_right_if(wstr, [](wchar_t wch) { return wch == L'й'; });
// or
size_t pos = wstr.find_last_not_of(L'й');
if (pos != std::wstring::npos)
   wstr.erase(pos + 1);
else
   wstr.clear();

Afterwards, you can also copy wstr back to mystring (assuming you make it non-const).

网友答案:
#include <iostream>
#include <string>

std::wstring wstrtrim(std::wstring const &w,std::wstring const &trim) {
    std::wstring::size_type first = w.find_first_not_of(trim);
    if (first!=std::wstring::npos)
      return w.substr(first, w.find_last_not_of(trim)-first+1);
    return L"";
}

int main() {
    const wchar_t mystring[] = L"зеленыйййййййййййййййййййййййй";
    std::wstring w = wstrtrim(mystring,L"й");
}
网友答案:

StrTrimW()

This is all you need for wchar_t replacement.

Art

相关阅读:
Top