0

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) ?

rsk82
  • 28,217
  • 50
  • 150
  • 240
  • 1
    the `const` in `const wchar_t *mystring = L"";` is needed because the pointer may point to read only memory, however if you use an array instead of a pointer then const isn't needed: `wchar_t mystring [] = L"";` because that syntax creates and initializes an array that has the normal automatic storage duration. – bames53 Jun 06 '12 at 17:13

3 Answers3

2

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).

eq-
  • 9,986
  • 36
  • 38
1
#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"й");
}
bames53
  • 86,085
  • 15
  • 179
  • 244
1

StrTrimW()

This is all you need for wchar_t replacement.

Art

art
  • 11
  • 1