CString类

注意:CString的定义位于afx.h中,是MFC的一部分。而不是C++的基础设施。

class CString
{
public:
// 构造方法


 CString(); // constructs empty CString

 CString(const CString& stringSrc); // copy constructor

 CString(TCHAR ch, int nRepeat = 1); // from a single character
 
 CString(LPCSTR lpsz);// from an ANSI string (converts to TCHAR)

 CString(LPCWSTR lpsz); // from a UNICODE string (converts to TCHAR)

 CString(LPCSTR lpch, int nLength); // subset of characters from an ANSI string (converts to TCHAR)

 CString(LPCWSTR lpch, int nLength); // subset of characters from a UNICODE string (converts to TCHAR)

 CString(const unsigned char* psz); // from unsigned characters

// 属性、操作


 int GetLength() const; // get data length

 BOOL IsEmpty() const; // TRUE if zero length

 void Empty(); // clear contents to empty

 
 TCHAR GetAt(int nIndex) const;// return single character at zero-based index

 TCHAR operator[](int nIndex) const; // return single character at zero-based index

 void SetAt(int nIndex, TCHAR ch); // set a single character at zero-based index

 operator LPCTSTR() const; // return pointer to const string

 // 重载


 const CString& operator=(const CString& stringSrc); // ref-counted copy from another CString

 const CString& operator=(TCHAR ch); // set string content to single character
#ifdef _UNICODE
 const CString& operator=(char ch);
#endif

 const CString& operator=(LPCSTR lpsz); // copy string content from ANSI string (converts to TCHAR)

 const CString& operator=(LPCWSTR lpsz); // copy string content from UNICODE string (converts to TCHAR)

 const CString& operator=(const unsigned char* psz); // copy string content from unsigned chars

 // 字符串连接


 const CString& operator+=(const CString& string); // concatenate from another CString


 const CString& operator+=(TCHAR ch); // concatenate a single character


#ifdef _UNICODE
 const CString& operator+=(char ch); // concatenate an ANSI character after converting it to TCHAR
#endif

 const CString& operator+=(LPCTSTR lpsz); // concatenate a UNICODE character after converting it to TCHAR

 friend CString AFXAPI operator+(const CString& string1,const CString& string2);
   
 friend CString AFXAPI operator+(const CString& string, TCHAR ch);
 friend CString AFXAPI operator+(TCHAR ch, const CString& string);
#ifdef _UNICODE
 friend CString AFXAPI operator+(const CString& string, char ch);
 friend CString AFXAPI operator+(char ch, const CString& string);
#endif
 friend CString AFXAPI operator+(const CString& string, LPCTSTR lpsz);
 friend CString AFXAPI operator+(LPCTSTR lpsz, const CString& string);

 // 字符串比较


 int Compare(LPCTSTR lpsz) const; // straight character comparison(直接进行字符比较)

 int CompareNoCase(LPCTSTR lpsz) const; // compare ignoring case(比较时不区分大小写)

 int Collate(LPCTSTR lpsz) const; // NLS aware comparison, case sensitive(国际化支持(Native Language Support)意识的比较,区分大小写)

 int CollateNoCase(LPCTSTR lpsz) const; // NLS aware comparison, case insensitive(国际化支持意识的比较,不区分大小写)

 // 简单子串提取


 CString Mid(int nFirst, int nCount) const; // return nCount characters starting at zero-based nFirst(取得从索引nFirst开始的其后nCount个字符)

 CString Mid(int nFirst) const; // return all characters starting at zero-based nFirst(取得从索引nFirst开始的其后所有字符)

 CString Left(int nCount) const; // return first nCount characters in string(取得字符串的前(左边)nCount个字符)

 CString Right(int nCount) const; // return nCount characters from end of string(取得字符串的后(右边)nCount个字符)


 CString SpanIncluding(LPCTSTR lpszCharSet) const; //  characters from beginning that are also in passed string(见标注)

 CString SpanExcluding(LPCTSTR lpszCharSet) const; // characters from beginning that are not also in passed string(见标注)

 // 大小写字母转换


 void MakeUpper(); // NLS aware conversion to uppercase(国际化支持意识的大写字母转换)

 void MakeLower(); // NLS aware conversion to lowercase(国际化支持意识的小写字母转换)

 void MakeReverse(); // reverse string right-to-left(字符串倒序转换)(如“hello”转换后为“olleh”)

 // 修剪空格 (either side)


 void TrimRight(); // remove whitespace starting from right edge(删除字符串右边缘的所有空格)

 void TrimLeft(); // remove whitespace starting from left side(删除字符串左边缘的所有空格)

 // 修剪其他 (either side)

 
 void TrimRight(TCHAR chTarget);// remove continuous occurrences of chTarget starting from right(删除字符串右边缘重复出现的chaTarget字符)

 void TrimRight(LPCTSTR lpszTargets); // remove continuous occcurrences of characters in passed string,starting from right(见标注)

 void TrimLeft(TCHAR chTarget); // remove continuous occurrences of chTarget starting from left(删除字符串左边缘重复出现的chaTarget字符)
 
 void TrimLeft(LPCTSTR lpszTargets);// remove continuous occcurrences of characters in passed string, starting from left(见标注)

 // 高级操作

//替换、插入、删除
 int Replace(TCHAR chOld, TCHAR chNew); // replace occurrences of chOld with chNew(用chNew字符代替字符串中出现的chOld字符)

 int Replace(LPCTSTR lpszOld, LPCTSTR lpszNew);// replace occurrences of substring lpszOld with lpszNew;empty lpszNew removes instances of lpszOld(用lpszNew子串代替lpszOld子串)

 int Remove(TCHAR chRemove); // remove occurrences of chRemove(移除字符串中出现的所有chRemove字符)

 int Insert(int nIndex, TCHAR ch); // insert character at zero-based index; concatenates if index is past end of string(在指定索引nIndex位置,插入ch字符(索引超出字符串范围,插入到字符串结尾))

 int Insert(int nIndex, LPCTSTR pstr); // insert substring at zero-based index; concatenates if index is past end of string(在指定的索引位置插入字符串子串)(索引超出字符串范围,插入到字符串结尾))

 int Delete(int nIndex, int nCount = 1); // delete nCount characters starting at zero-based index(从指定的索引位置开始,删除其后的nCount个字符)

 // 查找


 int Find(TCHAR ch) const; // find character starting at left, -1 if not found(从左边查找时字符ch第一次出现(最先被查找到)的索引,没找到返回-1)

 int ReverseFind(TCHAR ch) const; // find character starting at right(从右边查找时字符ch首次出现(最先被查找到)的索引(注意此处索引还是相对于左边起始位置),没有找到返回-1)
 
 int Find(TCHAR ch, int nStart) const;// find character starting at zero-based index and going right(从指定的nStart索引开始,向右查找指定字符ch,首次出现的索引)没有找到返回-1

 int FindOneOf(LPCTSTR lpszCharSet) const; // find first instance of any character in passed string(从左边开始找到首次出现“lpszCharSet中包含的任何一个字符”的字符并返回索引位置,没有找到返回-1)

 int Find(LPCTSTR lpszSub) const; // find first instance of substring(返回lpszSub指定的子串在字符串中起始位置的索引,如果字符串中不含有lpszSub子串,则返回-1)

 int Find(LPCTSTR lpszSub, int nStart) const; // find first instance of substring starting at zero-based index(从指定索引开始向右寻找lpszSub子串,并返回索引,如果没有找到子串,返回-1)

 // 简单格式化


 void AFX_CDECL Format(LPCTSTR lpszFormat, ...); // printf-like formatting using passed string(类似于printf的格式化输出,使用字符串)

 void AFX_CDECL Format(UINT nFormatID, ...); // printf-like formatting using referenced string resource(类似于printf的格式化,使用字符串资源ID)

 void FormatV(LPCTSTR lpszFormat, va_list argList); // printf-like formatting using variable arguments parameter(类似与printf的格式化,使用va_list等实现不定参函数到定参函数的转换)

 // 本地格式化(uses FormatMessage API)


 void AFX_CDECL FormatMessage(LPCTSTR lpszFormat, ...); // format using FormatMessage API on passed string(使用FormatMessage API将消息格式化到字符串)

 void AFX_CDECL FormatMessage(UINT nFormatID, ...); // format using FormatMessage API on referenced string resource(使用FormatMessage API将消息格式化到字符串)

 // 输入、输出
#ifdef _DEBUG
 friend CDumpContext& AFXAPI operator<<(CDumpContext& dc,const CString& string);
    
#endif
 friend CArchive& AFXAPI operator<<(CArchive& ar, const CString& string);
 friend CArchive& AFXAPI operator>>(CArchive& ar, CString& string);

 
 BOOL LoadString(UINT nID);// load from string resource(加载nID指定字符串资源)

#ifndef _UNICODE
 // ANSI <-> OEM support (convert string in place)


 void AnsiToOem(); // convert string from ANSI to OEM in-place

 void OemToAnsi(); // convert string from OEM to ANSI in-place
#endif

#ifndef _AFX_NO_BSTR_SUPPORT
 // OLE BSTR support (use for OLE automation)

 
 BSTR AllocSysString() const;// return a BSTR initialized with this CString's data

 BSTR SetSysString(BSTR* pbstr) const; // reallocates the passed BSTR, copies content of this CString to it
#endif

 // 获取字符串缓冲区作为C类型字符数组缓冲区


 LPTSTR GetBuffer(int nMinBufLength); // get pointer to modifiable buffer at least as long as nMinBufLength(获取至少长度为nMinBufLength的可修改缓冲区)

 void ReleaseBuffer(int nNewLength = -1); // release buffer, setting length to nNewLength (or to first nul if -1)(结束通过GetBuffer获得的缓冲区的使用)

 LPTSTR GetBufferSetLength(int nNewLength); // get pointer to modifiable buffer exactly as long as nNewLength(得到字符串的可修改缓冲区,长度恰好为nNewLength)
 
 void FreeExtra();// release memory allocated to but unused by string(释放分配给字符串但是没有使用的额外内存)

 // Use LockBuffer/UnlockBuffer to turn refcounting off


 LPTSTR LockBuffer(); // turn refcounting back on(使字符串处于被锁状态)

 void UnlockBuffer(); // turn refcounting off(释放被锁状态)

// Implementation
public:
 ~CString();
 int GetAllocLength() const;

protected:
 LPTSTR m_pchData;   // pointer to ref counted string data

 // implementation helpers
 CStringData* GetData() const;
 void Init();
 void AllocCopy(CString& dest, int nCopyLen, int nCopyIndex, int nExtraLen) const;
 void AllocBuffer(int nLen);
 void AssignCopy(int nSrcLen, LPCTSTR lpszSrcData);
 void ConcatCopy(int nSrc1Len, LPCTSTR lpszSrc1Data, int nSrc2Len, LPCTSTR lpszSrc2Data);
 void ConcatInPlace(int nSrcLen, LPCTSTR lpszSrcData);
 void CopyBeforeWrite();
 void AllocBeforeWrite(int nLen);
 void Release();
 static void PASCAL Release(CStringData* pData);
 static int PASCAL SafeStrlen(LPCTSTR lpsz);
 static void FASTCALL FreeData(CStringData* pData);
};

 

标注:

       1、CString SpanIncluding(LPCTSTR lpszCharSet) const; //  characters from beginning that are also in passed string

             CString SpanExcluding(LPCTSTR lpszCharSet) const; // characters from beginning that are not also in passed string

             SpanIncluding方法,从字符串中提取字符,lpszCharSet是一些字符的集合,当字符串的首字符包含在lpszCharSet时,该方法返回字符串中从首字符开始到“出现不在lpszCharSet集合中的”字符的前一个字符为止,构成的子串; 当lpszCharSet中不包含字符串的首字符,返回空字符串。

             SpanExcluding方法,搜索字符串找到第一个在lpszCharSet中也包含的任何一个字符,然后返回这个字符之前的子串,如果字符串中没有lpszCharSet包含的字符,该方法返回整个字符串(MSDN2010)(MSDN2001则不同,查看帮助还是使用最新版的好!)

       2、void TrimRight(LPCTSTR lpszTargets); // remove continuous occcurrences of characters in passed string,starting from right

            void TrimLeft(LPCTSTR lpszTargets);// remove continuous occcurrences of characters in passed string, starting from left

           TrimRight方法,从字符串右边查找连续出现的包含在lpszTargets字符集合中的字符构成的子串,然后剪切掉,如果右边沿的字符不在lpszTragets中,不剪切任何部分。(只要记住TrimRight、TrimLeft只能从边沿操作,不能从字符串内部操作即可);

          例如:CString str(_T("hello world accba"));  如果使用 str.TrimRight(_T("abc")); 剪切后str == _T(“hello world ”);如果使用str.TrimRight(_T("world")); 剪切后str == _T("hello world accba");(没有剪切掉任何部分)

           TrimLeft方法,与TrimRight类似,不同的是从左边沿操作。

      3、GetBuffer 与ReleaseBuffer

           LPTSTR GetBuffer(int nMinBufLength); LPTSTR GetBufferSetLength(int nNewLength);

          GetBuffer、GetBufferSetLength返回值都是指向字符串的指针(TCHAR*类型)。在使用该函数获得的缓冲区后,应该调用ReleaseBuffer来结束对缓冲区的使用。

          注意:在调用ReleaseBuffer函数之后或进行其他的CSting操作之后,GetBuffer获得的缓冲区地址就是无效的,不应该被使用。

         void ReleaseBuffer(int nNewLength = -1); 如果字符串是空终止的,可以忽略nNewLength参数,如果不是空终止的,通过这个参数指定字符串长度,为-1时,设置字符串的长度为当前长度。

          4、LockBuffer 和UnlockBuffer

         LockBuffer:通过调用LockBuffer,创建字符串副本,字符串引用计数设置为-1,缓冲区内字符串处于被锁状态,确保独占式使用该缓冲区中字符串,这时任何其他字符串不能引用该字符串,该字符串也不能引用其他字符串。

         注意:如果调用GetBuffer时设置的nMinBufLength的值大于当前缓冲区大小,那么当前缓冲去会被破坏掉,代替它的是一个nMinBufLength指定长度的新缓冲区(使用计数重置为0),因此LockBuffer将无效。

         

补充:

        1、Cstring与TCHAR 类型的转换:

         CString Cstr;

         TCHAR tChar = (LPCTSTR)Cstr;

        2、std::string 与 CString的转换

         CString Cstr; std::string  str;

         str = string((LPCTSTR)Cstr);

        3、将字符串转换为数字

        CString str = "123";

        int i = 0;

        sscanf(str, "%d", &i);

          

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值