帮帮忙,如何知道CString 字符串里是否有中文(或者非英文字母)

cpx 2005-08-10 04:45:31
如:
CString str="abc中文";
难道要每个ASC码这么核对啊!
有谁知道,先谢了!
...全文
323 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
laofang 2005-09-28
  • 打赏
  • 举报
回复
优化了一下:
BOOL HasWChar2(CString str)
{
if(str.IsEmpty())
return FALSE;

unsigned char *beg, *ori;
beg = ori = (unsigned char*)str.GetBuffer(0);
str.ReleaseBuffer();

unsigned char *tmp;
int bytes=0;
while((tmp = _mbsinc(beg)) <= beg + str.GetLength())
{
++bytes;
if(tmp-ori > bytes) // 字符长度大于字节长
return TRUE;

beg = tmp;
}

return FALSE;
}
flyelf 2005-09-28
  • 打赏
  • 举报
回复
IsDBCSLeadByte
laofang 2005-09-28
  • 打赏
  • 举报
回复
判断字串中是否包含宽字节字符(包括中、日等文字):
BOOL HasWChar(CString str)
{
unsigned char *beg = (unsigned char*)str.GetBuffer(0);
str.ReleaseBuffer();

int nByteLen = str.GetLength();
unsigned char *end = beg + nByteLen;

int nCharLen=0;
unsigned char *tmp;
while((tmp = _mbsinc(beg)) <= end)
{
nCharLen++;
beg = tmp;
}

if(nByteLen == nCharLen) // 字节数和字符数一致,则认为不包含宽字节字符
return FALSE;
else
return TRUE;
}
long_xing 2005-09-28
  • 打赏
  • 举报
回复
可以判断哪个字节是小于1的,就可以判定该字符是汉字的一部分。
summer54 2005-09-28
  • 打赏
  • 举报
回复
The IsDBCSLeadByteEx function determines whether a specified byte is a lead byte, that is, the first byte of a two-byte character in a double-byte character set (DBCS) for the specified code page.

BOOL IsDBCSLeadByteEx(
UINT CodePage, // identifier of code page
BYTE TestChar // byte to test
);
goodboyws 2005-09-28
  • 打赏
  • 举报
回复

int iLen =0;
while (iLen<str.GetLength())
{
if (IsDBCSLeadByteEx(936, str[iLen]))
{
CString hz = str.Mid(iLen, 2);
printf(hz);
iLen += 2;
}
else
iLen++;
}
phoenix96_2000 2005-09-28
  • 打赏
  • 举报
回复
英文字母:isalaph()
可打印字符: isprint()

至于判断是否是中文,和编码有关系,
dirdirdir3 2005-09-28
  • 打赏
  • 举报
回复
好像没有什么好办法,逐个核对也不麻烦阿。

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧