MFC获取字符串长度的5种方法

时间:2022-06-28 14:57:20
char s1[] = "中文ABC";

wchar_t s2[] = L"中文ABC";

 

1.sizeof() /*获取字符数组的字节数(包括结束符0)*/

         sizeof(s1) = 8;ANSI

         sizeof(s2) = 12;UNICODE

2.strlen/wcslen /*采取0作为字符串的结束符,并返回不包括0在内的字符数目*/

         strlen(s1) = 7;ANSI

         wcslen(s2) = 5;UNNICODE

3.GetLenth() /*返回字符而非字节数目*/

         CStringA sa = s1;ANSI

         CStringW sw = s2;UNICODE

         sa.GetLength() = 7;

         sw.GetLength() = 5;

4.size()与GetLength()一样

5.length() /*获取字符数目的最佳方案*/

         _bstr_t::length();

         _bstr_t bs1 = s1;

         _bstr_t bs2 = s2;

         bs1.length() = 5;ANSI

         bs2.length() = 5;UNICODE