字符串(五)进阶

前言:

本章主要介绍一下windows里面(或者说是MFC)里面 的CString类。

(想写几篇文章,深入讨论 CString, unicode, ascii, 宽字节,多字节这类问题。但发现自己水平不够。无法深刻理解,不知道如何介绍。

  接下来的几篇关于字符串、字符编码问题的文章,只是介绍一下我个人肤浅的理解。 )

简要介绍 CString 类

 1.MSDN关于CString的基本介绍

CString does not have a base class.

CString object consists of a variable-length sequence of characters. CString provides functions and operators using a syntax similar to that of Basic. Concatenation and comparison operators, together with simplified memory management, make CString objects easier to use than ordinary character arrays.

CString is based on the TCHAR data type. If the symbol _UNICODE is defined for your program, TCHAR is defined as type wchar_t, a 16-bit character type; otherwise, it is defined as char, the normal 8-bit character type. Under Unicode, then, CString objects are composed of 16-bit characters. Without Unicode, they are composed of 8-bit char type.

When not using _UNICODECString is enabled for multibyte character sets (MBCS, also known as double-byte character sets, DBCS). Note that for MBCS strings, CString still counts, returns, and manipulates strings based on 8-bit characters, and your application must interpret MBCS lead and trail bytes itself.

CString objects also have the following characteristics:

    • CString objects can grow as a result of concatenation operations.

    • CString objects follow “value semantics.” Think of a CString object as an actual string, not as a pointer to a string. 

    • You can freely substitute CString objects for const char* and LPCTSTR function arguments.

    • A conversion operator gives direct access to the string’s characters as a read-only array of characters (a C-style string).

Several forms of the constructor have special purposes:(CString 强大的构造函数)

    • CString( LPCSTR lpsz )   Constructs a Unicode CString from an ANSI string. You can also use this constructor to load a string resource as shown in the example below.

    • CString( LPCWSTR lpsz )   Constructs a CString from a Unicode string.

    • CString( const unsigned char* psz )   Allows you to construct a CString from a pointer to unsigned char

 

2.个人理解CString

2.1工程环境是 unicode 编码还是 ASCII编码

在Windows编程中,CString 类里面的字符串,是 Unicode还是 ASCII,是和你的编译器的环境相关的。

如果你编译器是 ASCII 环境,那么CString里面的字符串就是 ASCII ;如果你编译器是Unicode,那么CString里面的字符串就是 unicode的;

2.2编译器 设置的 编码 环境

在vs2013,以及vs2013以后(vs2015),工程中默认就是 unicode编码了;

vs2012,以及vs2012(vs2010, vs2008...)以前,工程中的字符编码,是否选择 unicode 是可以勾选的;

vc6.0默认的编码是ASCII的。

2.3CString, CStringA, CStringW 

CString 是 环境相关的(环境如果是unicode, CString就等同于 CStringW; 环境如果是ASCII, CString就等同于CStringA); 

 

CStringA, 用来存储 ASCII 编码的字符串的类

CStringW 用来存储 unicode 编码的字符串的类;

2.4CString 里面包含了丰富的构造函数,它赋予CString强大的能力。你可以通过 CString 的构造函数,实现各种字符的转换。

例如:

CStringW  strW = L"我是谁";  /// unicode 编码的字符串

CStringA  strA(strW);   /// 转为了 ascii编码的字符串了

通过CString 强大的构造函数,就实现了将 unicode 编码的字符串,转为 ASCII编码的字符串了。(你可能觉得这种方法很愚蠢,但这个功能很实用的)

3.CString 类的用法

在使用 CString 类的时候,需要 包含 <afx.h> 头文件(win32工程需要包含这个头文件。如果你是MFC的工程,就不必包含。因为MFC的工程已经自动包含了这个头文件) 
1     CStringA strA1 = "晚上好";
2     CStringA strA2("现在不早了");
3     char szStr1[100] = {0};
4     sprintf(szStr1,"%s", strA1.GetString());  GetString() 方法,得到字符串的指针地址。可以通过这种方式,将字符串拷贝出来。
5     strA2.Format(" **** %s", szStr1);   /// Format() 方法,格式化字符串。

因为我是在vs2013下写的代码,所以为了介绍CString,就用了CStringA 这个类了。

下面介绍CStringW.

如何初始化CStringW?

在进行格式化的时候,必须加上 “ L ”,来表示宽字符。

1     CStringW strW1 = "晚上好";  /// 你可能会问,为啥这里没有加 L , 因为 CString 类很强大,在此处初始化的时候,有类型转换。但是还是建议在 CStringW 的字符串前面,加上 L ,表示这是一个 宽字符的字符串。
2     CStringW strW2("现在不早了");
3     wchar_t szStr1[100] = {0};
4     wsprintf(szStr1,L"%s", strW1.GetString());
5     strW2.Format(L" **** %s", szStr1);

小结一下:

理论太过深奥,本人理解也不深刻。

重点是下面的:

1.定义 unicode 的CString 的时候,记得在字符串前面加 L . 

2.unicode 下面是 用 wsprintf() 来进行 格式化 输出的(掌握了sprintf()的用法,wsprintf()的用法就类似了);

3.熟悉 CString 的 GetString(), GetLengt(), Format() 函数;

4.unicode 编码的字符串,需要定义为 wchar_t 类型的哦。 
 
 

转载于:https://www.cnblogs.com/MasterOogway/p/5801303.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值