检查Cstring是否只包含中文字符 [英] Check whether a Cstring contains only Chinese characters

查看:613
本文介绍了检查Cstring是否只包含中文字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我检查CString变量是否只包含]汉字。中文字符的Unicode范围是4E00 - 9FFF。

I am checking whether a CString variable contains only] Chinese characters. The Unicode range for Chinese characters is 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')

所以我的问题是,如何查找一个字符位于特定Unicode范围之间的天气?

So my question is, how to find weather a character lies between a particular Unicode range?

另一件事:如果我使用 if(ch =='\\\一'),那么它就是true, c $ c> if(ch <='\\\一')它返回false。我不明白这个行为!

One more thing: if I use if(ch=='\u4e00') then it gives true, but if I do if(ch<='\u4e00') it returns false. I don't understand this behavior!

我的代码是

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


推荐答案

它可以比较如下:

CString str;
wchar_t ch;
GetDlgItemText( IDC_EDIT1, str );

for(int i=0;i<str.GetLength();i++) {
  ch=str[i];
  if((unsigned int)ch>=0x4E00u&&(unsigned int)ch<=ox9FFFu) {
    SetDlgItemText( IDC_RICHEDIT21, str);
    SendDlgItemMessage( IDC_RICHEDIT21, EM_REPLACESEL, TRUE, (LPARAM)(LPCTSTR)str);
  } else
    break;

这篇关于检查Cstring是否只包含中文字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆