哪位大侠知道怎么得到字符串的字符数(不是字节数)(50)

zaghost 2004-10-10 09:26:08
哪位大侠知道怎么得到字符串的字符数(不是字节数)(50)
如:得到 CString str="a人" 的字符数
...全文
305 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
技术上可行
符合你的要求吗?
如果不明白,你可以
(1),给我发短消息(请附帖子的地址).
(2),根据http://www.vcshare.net上的联系方式与我联系,推荐QQ.
(3),如果问题简单,请在http://www.vcshare.net/bbs上发帖.
如果问题比较复杂,请把代码压成*.rar当附件发在我的论坛上发帖.
同时在www.csdn.net上发帖,并给出在我的论坛的链接.
---------------
压代码前,请先删除debug,release文件夹,及*.opt,*.ncb,*.plg,*.aps.
  • 打赏
  • 举报
回复
void CDlgcsdn20Dlg::OnButton3()
{
CString str = "3个中国人!";

int nLen = str.GetLength();//10

nLen = MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,str,-1,NULL,0) - 1;//6

}
fcf128 2004-10-11
  • 打赏
  • 举报
回复
CString.GetLength() 行了。我4过
Mr-Chen 2004-10-11
  • 打赏
  • 举报
回复
GetLength
yo_jo 2004-10-11
  • 打赏
  • 举报
回复
是呀,要得到字符数比字节数要简单呀,是不是题目有问题?
huwei001982 2004-10-11
  • 打赏
  • 举报
回复
转换成 UniCode, 再除以二
ohbaby 2004-10-11
  • 打赏
  • 举报
回复
mark一下
smallcrocodile 2004-10-11
  • 打赏
  • 举报
回复
GetLengthb()
就是了,汉字也看作一个字符
ldsjlzy 2004-10-11
  • 打赏
  • 举报
回复
//功能:字符串字数计算
int StrLenEx(CString &strText)
{
int nByte = strText.GetLength() ;
int nWord = 0 ;
for( int i=0; i<nByte; i++ )
{
nWord ++ ;
if( IsDBCSLeadByte( strText[i] ) )
i ++ ;
}
return nWord ;
}
alon21 2004-10-10
  • 打赏
  • 举报
回复
#include "COMDEF.H"

CString str="a人";
_bstr_t strTemp;
strTemp=str;
int len=strTemp.length();
memory_xj 2004-10-10
  • 打赏
  • 举报
回复
自己编写一个不就得了:
unsigned char c1,c2;
CString sSource("如果用UNICODE,怎么处理?"); //一个假设的测试字体串
int iLength = sSource.GetLength();
long lZfCount = 0; //字符数
int i = 0;
while ( i < iLength)
{
c1 = sSource.GetAt(i);
c2 = sSource.GetAt(i + 1);
if (c1 > 127 && c2 > 127) //如果是汉字,则是双字节
i += 2;
else //如果是一般字符,前进一字节
i ++;
lZfCount ++;
}
//循环结束后的lZfCount就是字符数
zaghost 2004-10-10
  • 打赏
  • 举报
回复
如果用UNICODE,怎么处理?
3x
bluebohe 2004-10-10
  • 打赏
  • 举报
回复
_mbslen((const unsigned char*)(LPCTSTR)str);
lovenoend 2004-10-10
  • 打赏
  • 举报
回复
转换成Unicode
zaghost 2004-10-10
  • 打赏
  • 举报
回复
up

CString str="a人" 得到2而不是3
xindao_wang 2004-10-10
  • 打赏
  • 举报
回复
CString::GetLength好像不行,楼主的意思是要字符数,一个汉字字符应该由两个字节组成。

bluebohe 2004-10-10
  • 打赏
  • 举报
回复
_mbslen and _mbstrlen return the number of multibyte characters in a multibyte-character string. _mbslen recognizes multibyte-character sequences according to the multibyte code page currently in use; it does not test for multibyte-character validity. _mbstrlen tests for multibyte-character validity and recognizes multibyte-character sequences according to the LC_CTYPE category setting of the current locale. For more information about the LC_CTYPE category, see setlocale.

星光依旧灿烂 2004-10-10
  • 打赏
  • 举报
回复
使用
int char_number=str.GetLength();
汉字占两个字符
Mr_Ldh 2004-10-10
  • 打赏
  • 举报
回复
转换成Unicode再用wcslen()
李马 2004-10-10
  • 打赏
  • 举报
回复
CString::GetLength
或者lstrlen/strlen

16,473

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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