简体   繁体   中英

How can I get one character from cyrillic string c++

I have wstring with cyrillic word. I need to get one letter from it. I found only this way:

wstring line;
wifstream myfile (".../outfile.txt");
if (myfile.is_open())
{
    while (myfile.good())
    {
        getline (myfile,line);
        wstring a = line.substr(0,2); // this gives one first letter
       //....
    }
    myfile.close();
}

Are there better ways to get a letter from cyrillic string?

If cyrillic uses surrogate pairs in UTF-16 encoding, instead of doing this:

wstring a = line.substr(0,2);

you might want to consider doing something similar to this:

const wchar_t surrogate[] = { line[0], line[1], L'\0' };
const wchar_t non_surrogate[] = { line[0], L'\0' };
const wstring a = IS_SURROGATE_PAIR(surrogate[0], surrogate[1]) ?
                  surrogate :
                  non_surrogate; 

The IS_SURROGATE_PAIR macro is for Windows - if you are elsewhere you can do the check yourself by reading up on the macro link and on its accompanying Surrogates and Supplementary Characters docs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM