c++ - 我如何从西里尔字符串中获取一个字符 C++

标签 c++

我有带有西里尔字母的 wstring。我需要从它那里得到一封信。 我发现只有这样:

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();
}

是否有更好的方法从西里尔字符串中获取字母?

最佳答案

如果西里尔字母在 UTF-16 编码中使用代理对,而不是这样做:

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

你可能想考虑做类似的事情:

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; 

IS_SURROGATE_PAIR宏适用于 Windows - 如果您在其他地方,您可以通过阅读宏链接及其随附的 Surrogates and Supplementary Characters 自行检查。文档。

关于c++ - 我如何从西里尔字符串中获取一个字符 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28849976/

相关文章:

c++ - C++ 中嵌套结构的模板

c++ - 如果我希望stream_iterator不忽略空格该怎么办

c++ - 如果在其他函数内部调用的函数抛出异常会怎样?

c++ - 在 Visual Studio 中运行教程 - 什么样的 "New Project"?

c++ - 如何使用软件程序(如yahoo messenger)向手机发送短信?

c++ - 如何通过按 "cancel"键取消 CListCtrl 中的编辑?

c++ - 空char数组的初始化是否有效?

c++ - 16个嵌套循环速度C++/Rcpp

c++ - 检查 CUDA 内核中的 device_vector 不起作用

c++ - 如何将文件夹删除到回收站