MFC-统计编辑框中的字符串长度和字符个数

VS2012

多字节字符集

Edit Control文本框设置成多行(右键属性-Multiline改为true)

启用回车Enter(右键属性-Want Return改为true)

添加水平滚动条(右键属性-Horizontal Scroll改为true)

添加垂直滚动条(右键属性-Vertical Scroll改为true)

 1 void CMFCApplication1Dlg::OnBnClickedButtonCount()
 2 {
 3     // TODO: 在此添加控件通知处理程序代码
 4 
 5     //获得文本框里的文本
 6     CString strInput;
 7     GetDlgItemText(IDC_EDIT_INPUT, strInput);
 8 
 9     //获得字符串的长度
10     int nLen = strInput.GetLength();
11 
12     //判断字符是中文还是英文
13     int nCount = 0;
14     for (int i = 0; i < nLen; i++)
15     {
16         //如果有一个是中文,那么字符就会大于7f
17         if ((BYTE)strInput[i] > 0x7f)
18         {
19             ++i;//略过中文
20         }
21         nCount++;//个数
22     }
23 
24     //输出
25     CString strOutput;
26     strOutput.Format(_T("字符串长度:%d, 字符个数:%d"), nLen, nCount);
27     SetDlgItemText(IDC_STATIC_OUTPUT, strOutput);
28 }

Caesar卢尚宇

2021年1月4日

原文地址:https://www.cnblogs.com/nxopen2018/p/14232939.html