【发布时间】:2013-04-26 03:34:14
【问题描述】:
我正在检查一个 CString 变量是否只包含] 中文字符。汉字的 Unicode 范围是 4E00 - 9FFF。
我的做法如下:
CString str;
char ch;
GetDlgItemText( IDC_EDIT1, str );
for(int i=0;i<str.GetLength();i++) {
ch=str[i];
if(ch>='\u4E00'&&ch<='\u9FFF') {
//even if input chinese character here 'if' evaluates to false
SetDlgItemText( IDC_RICHEDIT21, str );
SendDlgItemMessage( IDC_RICHEDIT21, EM_REPLACESEL, TRUE, (LPARAM)(LPCTSTR)str);
} else
break;
但如果我这样做了
if(ch=='\u4E00')
输入\u4E00的符号就可以了。
所以我的问题是,如何查找字符位于特定 Unicode 范围之间的天气?
还有一件事:如果我使用if(ch=='\u4e00'),那么它会返回true,但如果我使用if(ch<='\u4e00'),它会返回false。我不明白这种行为!
我的代码是
CString str;
wchar_t ch;
GetDlgItemText( IDC_EDIT1, str );
for(int i=0;i<str.GetLength();i++) {
ch=str[i];
if(ch<='\u4e01') {
//returns false, but returns true if(ch=='\u4e01')
SetDlgItemText( IDC_RICHEDIT21, str );
SendDlgItemMessage( IDC_RICHEDIT21, EM_REPLACESEL, TRUE, (LPARAM)(LPCTSTR)str);
else
break;
}
【问题讨论】:
-
嗯,你用的是char,不是wchar。
-
如果我使用 wchar 我得到如下错误,test3Dlg.cpp(155): error C2065: 'wchar' : undeclared identifier
-
WCHAR(由 Windows 标头定义),或wchar_t使用 C++ 类型。 -
这是中日韩统一表意文字的范围。不要忘记 CJK 部首补充、康熙部首、CJK 符号和标点、CJK 统一表意文字扩展 A、CJK 兼容表意文字、CJK 统一表意文字扩展 B、CJK 统一表意文字扩展 C、CJK 统一表意文字扩展 D 或 CJK 兼容表意文字补充。请注意,其中一些高于 U+FFFF。不要忘记即将推出的 CJK Unified Ideographs Extension E 或 F,它们还没有代码点。您可能需要重新考虑您要完成的工作。
-
您忽略了来自编译器的警告。不。铸造只会给你挖一个更深的洞。宽字符文字需要在前面加上 L,例如
L'\u4e00'