This sounds like a simple problem, but C++ is making it difficult (for me at least): I have a wstring and I would like to get the first letter as a wchar_t object and then remove this first letter from the string.
This here does not work for non-ASCII characters:
wchar_t currentLetter = word.at(0); Because it returns two characters (in a loop) for characters such as German Umlauts.
This here does not work, either:
wchar_t currentLetter = word.substr(0,1); error: no viable conversion from 'std::basic_string<wchar_t>' to 'wchar_t' And neither does this:
wchar_t currentLetter = word.substr(0,1).c_str(); error: cannot initialize a variable of type 'wchar_t' with an rvalue of type 'const wchar_t *' Any other ideas?
Cheers,
Martin
---- Update ----- Here is some executable code that should demonstrate the problem. This program will loop over all letters and output them one by one:
#include <iostream> using namespace std; int main() { wstring word = L"für"; wcout << word << endl; wcout << word.at(1) << " " << word[1] << " " << word.substr(1,1) << endl; wchar_t currentLetter; bool isLastLetter; do { isLastLetter = ( word.length() == 1 ); currentLetter = word.at(0); wcout << L"Letter: " << currentLetter << endl; word = word.substr(1, word.length()); // remove first letter } while (word.length() > 0); return EXIT_SUCCESS; } However, the actual output I get is:
f?r ? ? ? Letter: f Letter: ? Letter: r
The source file is encoded in UTF8 and the console's encoding is also set to UTF8.
wstring::substr()returns a newwstring, not a single character.