转至:http://www.cnblogs.com/Caiqinghua/archive/2009/02/16/1391190.html
CString的构造函数<wbr><wbr><br></wbr></wbr>CString( );
例:CString csStr;
CString( const CString& stringSrc );
例:CString csStr("ABCDEF中文123456");
CString csStr2(csStr);
CString( TCHAR ch, int nRepeat = 1 );
例:CString csStr('a',5);
//csStr="aaaaa"<wbr><br><br> CString( LPCTSTR lpch, int nLength );<br> 例:CString csStr("abcdef",3);<br><span style="font-size:14px; color:#990000; line-height:1.5em">//csStr="abc"</span><wbr><br><br> CString( LPCWSTR lpsz );<br> 例:wchar_t s[]=L"abcdef";<br> CString csStr(s);<br><span style="font-size:14px; color:#990000; line-height:1.5em">//csStr=L"abcdef"</span><wbr><br><br> CString( const unsigned char* psz );<br> 例:const unsigned char s[]="abcdef";<br> const unsigned char* sp=s;<br> CString csStr(sp);<br><span style="font-size:14px; color:#990000; line-height:1.5em">//csStr="abcdef"</span><wbr><br><br> CString( LPCSTR lpsz );<br> 例:CString csStr("abcdef");<br><span style="font-size:14px; color:#990000; line-height:1.5em">//csStr="abcdef"</span><wbr><br><br><span style="font-size:14px; color:#0000cc; line-height:1.5em"><strong><wbr>int GetLength( ) const;</wbr></strong><wbr></wbr></span><wbr><br> 返回字符串的长度,不包含结尾的空字符。<br> 例:csStr="ABCDEF中文123456";<br> printf("%d",csStr.GetLength()); <span style="font-size:14px; color:#990000; line-height:1.5em"> //16</span><wbr><br><br><span style="font-size:14px; color:#0000cc; line-height:1.5em"><strong><wbr>void MakeReverse( );</wbr></strong><wbr></wbr></span><wbr><br> 颠倒字符串的顺序<br> 例:csStr="ABCDEF中文123456";<br> csStr.MakeReverse();<br> cout<<csStr;<span style="font-size:14px; color:#990000; line-height:1.5em">//654321文中FEDCBA</span><wbr><br><br><strong><wbr><span style="font-size:14px; color:#0000cc; line-height:1.5em">void MakeUpper( );</span><wbr></wbr></wbr></strong><wbr><br> 将小写字母转换为大写字母<br> 例:csStr="abcdef中文123456";<br> csStr.MakeUpper();<br> cout<<csStr;<span style="font-size:14px; color:#990000; line-height:1.5em">//ABCDEF中文123456</span><wbr><br><br><span style="font-size:14px; color:#0000cc; line-height:1.5em"><strong><wbr>void MakeLower( );</wbr></strong><wbr></wbr></span><wbr><br> 将大写字母转换为小写字母<br> 例:csStr="ABCDEF中文123456";<br> csStr.MakeLower();<br> cout<<csStr;<span style="font-size:14px; color:#990000; line-height:1.5em">//abcdef中文123456</span><wbr><br><br><span style="font-size:14px; color:#0000cc; line-height:1.5em"><strong><wbr>int Compare( LPCTSTR lpsz ) const;</wbr></strong><wbr></wbr></span><wbr><br> 区分大小写比较两个字符串,相等时返回0,大于时返回1,小于时返回-1<br> 例:csStr="abcdef中文123456";<br> csStr2="ABCDEF中文123456";<br> cout<<csStr.CompareNoCase(csStr2); <span style="font-size:14px; color:#990000; line-height:1.5em"> //0</span><wbr><br><br><span style="font-size:14px; color:#0000cc; line-height:1.5em"><strong><wbr>int CompareNoCase( LPCTSTR lpsz ) const;</wbr></strong><wbr></wbr></span><wbr><br> 不区分大小写比较两个字符串,相等时返回0,大于时返回1,小于时返回-1<br> 例:csStr="abcdef中文123456";<br> csStr2="ABCDEF中文123456";<br> cout<<csStr.CompareNoCase(csStr2); <span style="font-size:14px; color:#990000; line-height:1.5em"> //-1</span><wbr><br><br><span style="font-size:14px; color:#0000cc; line-height:1.5em"><strong><wbr>int Delete( int nIndex, int nCount = 1 )</wbr></strong><wbr></wbr></span><wbr><br> 删除字符,删除从下标nIndex开始的nCount个字符<br> 例:csStr="ABCDEF";<br> csStr.Delete(2,3);<br> cout<<csStr;<span style="font-size:14px; color:#990000; line-height:1.5em">// ABF<br> //当nIndex过大,超出对像所在内存区域时,函数没有任何操作。<br> //当nIndex为负数时,从第一个字符开始删除。<br> //当nCount过大,导致删除字符超出对像所在内存区域时,会发生无法预料的结果。<br> //当nCount为负数时,函数没有任何操作。</span><wbr><br><br><span style="font-size:14px; color:#0000cc; line-height:1.5em"><strong><wbr>int Insert( int nIndex, TCHAR ch )<br> int Insert( int nIndex, LPCTSTR pstr )</wbr></strong><wbr></wbr></span><wbr><br> 在下标为nIndex的位置,插入字符或字符串。返回插入后对象的长度<br> 例:csStr="abc";<br> csStr.Insert(2,'x');<br> cout<<csStr;<span style="font-size:14px; color:#990000; line-height:1.5em"> //abxc<br></span><wbr>csStr="abc";<br> csStr.Insert(2,"xyz");<br> cout<<csStr;<span style="font-size:14px; color:#990000; line-height:1.5em"> //abxyzc</span><wbr><br><span style="font-size:14px; color:#990000; line-height:1.5em">//当nIndex为负数时,插入在对象开头<br> //当nIndex超出对象末尾时,插入在对象末尾</span><wbr><br><br><span style="font-size:14px; color:#0000cc; line-height:1.5em"><strong><wbr>int Remove( TCHAR ch );</wbr></strong><wbr></wbr></span><wbr><br> 移除对象内的指定字符。返回移除的数目<br> 例:csStr="aabbaacc";<br> csStr.Remove('a');<br> cout<<csStr;<span style="font-size:14px; color:#990000; line-height:1.5em"> //bbcc</span><wbr><br><br><span style="font-size:14px; color:#0000cc; line-height:1.5em"><strong><wbr>int Replace( TCHAR chOld, TCHAR chNew );<br> int Replace( LPCTSTR lpszOld, LPCTSTR lpszNew );</wbr></strong><wbr></wbr></span><wbr><br> 替换字串<br> 例:csStr="abcdef";<br> csStr.Replace('a','x');<br> cout<<csStr;<span style="font-size:14px; color:#990000; line-height:1.5em">//xbcdef</span><wbr><br> csStr="abcdef";<br> csStr.Replace("abc","xyz");<br> cout<<csStr;<span style="font-size:14px; color:#990000; line-height:1.5em">//xyzdef</span><wbr><br><br><span style="font-size:14px; color:#0000cc; line-height:1.5em"><strong><wbr>void TrimLeft( );<br> void TrimLeft( TCHAR chTarget );<br> void TrimLeft( LPCTSTR lpszTargets );</wbr></strong><wbr></wbr></span><wbr><br> 从左删除字符,被删的字符与chTarget或lpszTargets匹配,一直删到第一个不匹配的字符为止<br> 例:csStr="aaabaacdef";<br> csStr.TrimLeft('a');<br> cout<<csStr;<span style="font-size:14px; color:#990000; line-height:1.5em">//baacdef<br></span><wbr>csStr="aaabaacdef";<br> csStr.TrimLeft("ab");<br> cout<<csStr;<span style="font-size:14px; color:#990000; line-height:1.5em">//cdef</span><wbr><br><span style="font-size:14px; color:#990000; line-height:1.5em">//无参数时删除空格</span><wbr><br><br><span style="font-size:14px; color:#0000cc; line-height:1.5em"><strong><wbr>void TrimRight( );<br> void TrimRight( TCHAR chTarget );<br> void TrimRight( LPCTSTR lpszTargets );</wbr></strong><wbr></wbr></span><wbr><br> 从右删除字符,被删的字符与chTarget或lpszTargets匹配,一直删到第一个不匹配的字符为止<br> 例:csStr="abcdeaafaaa";<br> csStr.TrimRight('a');<br> cout<<csStr; <span style="font-size:14px; color:#990000; line-height:1.5em"> //abcdeaaf</span><wbr><br> csStr="abcdeaafaaa";<br> csStr.TrimRight("fa");<br> cout<<csStr;<span style="font-size:14px; color:#990000; line-height:1.5em">//abcde<br> //无参数时删除空格</span><wbr><br><br><span style="font-size:14px; color:#0000cc; line-height:1.5em"><strong><wbr>void Empty( );</wbr></strong><wbr></wbr></span><wbr><br> 清空<br> 例:csStr="abcdef";<br> csStr.Empty();<br> printf("%d",csStr.GetLength());<span style="font-size:14px; color:#990000; line-height:1.5em">//0</span><wbr><br><br><span style="font-size:14px; color:#0000cc; line-height:1.5em"><strong><wbr>BOOL IsEmpty( ) const;</wbr></strong><wbr></wbr></span><wbr><br> 测试对象是否为空,为空时返回零,不为空时返回非零<br> 例:csStr="abc";<br> cout<<csStr.IsEmpty(); <span style="font-size:14px; color:#990000; line-height:1.5em"> //0;<br></span><wbr>csStr.Empty();<br> cout<<csStr.IsEmpty(); <span style="font-size:14px; color:#990000; line-height:1.5em"> //1;</span><wbr><br><br><span style="font-size:14px; color:#0000cc; line-height:1.5em"><strong><wbr>int Find( TCHAR ch ) const;<br> int Find( LPCTSTR lpszSub ) const;<br> int Find( TCHAR ch, int nStart ) const;<br> int Find( LPCTSTR pstr, int nStart ) const;</wbr></strong><wbr></wbr></span><wbr><br> 查找字串,nStart为开始查找的位置。未找到匹配时返回-1,否则返回字串的开始位置<br> 例:csStr="abcdef";<br> cout<<csStr.Find('b'); <span style="font-size:14px; color:#990000; line-height:1.5em"> //1<br></span><wbr>cout<<csStr.Find("de");<span style="font-size:14px; color:#990000; line-height:1.5em">//3</span><wbr><br> cout<<csStr.Find('b',3); <span style="font-size:14px; color:#990000; line-height:1.5em"> //-1</span><wbr><br> cout<<csStr.Find('b',0); <span style="font-size:14px; color:#990000; line-height:1.5em"> //1</span><wbr><br> cout<<csStr.Find("de",4);<span style="font-size:14px; color:#990000; line-height:1.5em">//-1</span><wbr><br> cout<<csStr.Find("de",0);<span style="font-size:14px; color:#990000; line-height:1.5em">//3</span><wbr><br><span style="font-size:14px; color:#990000; line-height:1.5em">//当nStart超出对象末尾时,返回-1。<br> //当nStart为负数时,返回-1。</span><wbr><br><br><span style="font-size:14px; color:#0000cc; line-height:1.5em"><strong><wbr>int FindOneOf( LPCTSTR lpszCharSet ) const;</wbr></strong><wbr></wbr></span><wbr><br> 查找lpszCharSet中任意一个字符在CString对象中的匹配位置。未找到时返回-1,否则返回字串的开始位置<br> 例:csStr="abcdef";<br> cout<<csStr.FindOneOf("cxy");<span style="font-size:14px; color:#990000; line-height:1.5em"> //2</span><wbr><br><br><span style="font-size:14px; color:#0000cc; line-height:1.5em"><strong><wbr>CString SpanExcluding( LPCTSTR lpszCharSet ) const;</wbr></strong><wbr></wbr></span><wbr><br> 返回对象中与lpszCharSet中任意匹配的第一个字符之前的子串<br> 例:csStr="abcdef";<br> cout<<csStr.SpanExcluding("cf");<span style="font-size:14px; color:#990000; line-height:1.5em">//ab</span><wbr><br><br><span style="font-size:14px; color:#0000cc; line-height:1.5em"><strong><wbr>CString SpanIncluding( LPCTSTR lpszCharSet ) const;</wbr></strong><wbr></wbr></span><wbr><br> 从对象中查找与lpszCharSe中任意字符不匹配的字符,并返回第一个不匹配字符之前的字串<br> 例:csStr="abcdef";<br> cout<<csStr.SpanIncluding("fdcba");<span style="font-size:14px; color:#990000; line-height:1.5em">//abcd</span><wbr><br><br><span style="font-size:14px; color:#0000cc; line-height:1.5em"><strong><wbr>int ReverseFind( TCHAR ch ) const;</wbr></strong><wbr></wbr></span><wbr><br> 从后向前查找第一个匹配,找到时返回下标。没找到时返回-1<br> 例:csStr="abba";<br> cout<<csStr.ReverseFind('a');<span style="font-size:14px; color:#990000; line-height:1.5em">//3</span><wbr><br><br><span style="font-size:14px; color:#0000cc; line-height:1.5em"><strong><wbr>void Format( LPCTSTR lpszFormat, ... );<br> void Format( UINT nFormatID, ... );</wbr></strong><wbr></wbr></span><wbr><br> 格式化对象,与C语言的sprintf函数用法相同<br> 例:csStr.Format("%d",13);<br> cout<<csStr; <span style="font-size:14px; color:#990000; line-height:1.5em"> //13</span><wbr><br><br><span style="font-size:14px; color:#0000cc; line-height:1.5em"><strong><wbr>TCHAR GetAt( int nIndex ) const;</wbr></strong><wbr></wbr></span><wbr><br> 返回下标为nIndex的字符,与字符串的[]用法相同<br> 例:csStr="abcdef";<br> cout<<csStr.GetAt(2); <span style="font-size:14px; color:#990000; line-height:1.5em"> //c<br> //当nIndex为负数或超出对象末尾时,会发生无法预料的结果。</span><wbr><br><br><span style="font-size:14px; color:#0000cc; line-height:1.5em"><strong><wbr>void SetAt( int nIndex, TCHAR ch );</wbr></strong><wbr></wbr></span><wbr><br> 给下标为nIndex的字符重新赋值<br> 例:csStr="abcdef";<br> csStr.SetAt(2,'x');<br> cout<<csStr;<span style="font-size:14px; color:#990000; line-height:1.5em">//abxdef<br> //当nIndex为负数或超出对象末尾时,会发生无法预料的结果。</span><wbr><br><br><span style="font-size:14px; color:#0000cc; line-height:1.5em"><strong><wbr>CString Left( int nCount ) const;</wbr></strong><wbr></wbr></span><wbr><br> 从左取字串<br> 例:csStr="abcdef";<br> cout<<csStr.Left(3);<span style="font-size:14px; color:#990000; line-height:1.5em"> //abc<br> //当nCount等于0时,返回空。<br> //当nCount为负数时,返回空。<br> //当nCount大于对象长度时,返回值与对象相同。</span><wbr><br><br><span style="font-size:14px; color:#0000cc; line-height:1.5em"><strong><wbr>CString Right( int nCount ) const;</wbr></strong><wbr></wbr></span><wbr><br> 从右取字串<br> 例:csStr="abcdef";<br> cout<<csStr.Right(3); <span style="font-size:14px; color:#990000; line-height:1.5em"> //def<br> //当nCount等于0时,返回空。<br> //当nCount为负数时,返回空。<br> //当nCount大于对象长度时,返回值与对象相同。</span><wbr><br><br><span style="font-size:14px; color:#0000cc; line-height:1.5em"><strong><wbr>CString Mid( int nFirst ) const;<br> CString Mid( int nFirst, int nCount ) const;</wbr></strong><wbr></wbr></span><wbr><br> 从中间开始取字串<br> 例:csStr="abcdef";<br> cout<<csStr.Mid(2); <span style="font-size:14px; color:#990000; line-height:1.5em"> //cdef<br></span><wbr>csStr="abcdef";<br> cout<<csStr.Mid(2,3); <span style="font-size:14px; color:#990000; line-height:1.5em"> //cde<br> //当nFirst为0和为负数时,从第一个字符开始取。<br> //当nFirst等于对象末尾时,返回空字串。<br> //当nFirst超出对象末尾时,会发生无法预料的结果。<br> //当nCount超出对象末尾时,返回从nFirst开始一直到对象末尾的字串<br> //当nCount为0和为负数时,返回空字串。</span><wbr><br><br><span style="font-size:14px; color:#0000cc; line-height:1.5em"><strong><wbr>LPTSTR GetBuffer( int nMinBufLength );</wbr></strong><wbr></wbr></span><wbr><br> 申请新的空间,并返回指针<br> 例:csStr="abcde";<br> LPTSTR pStr=csStr.GetBuffer(10);<br> strcpy(pStr,"12345");<br> csStr.ReleaseBuffer();<br> pStr=NULL;<br> cout<<csStr<span style="font-size:14px; color:#990000; line-height:1.5em"> //12345<br> //使用完GetBuffer后,必须使用ReleaseBuffer以更新对象内部数据,否则会发生无法预料的结果。</span><wbr><br><br><span style="font-size:14px; color:#0000cc; line-height:1.5em"><strong><wbr>void ReleaseBuffer( int nNewLength = -1 );</wbr></strong><wbr></wbr></span><wbr><br> 使用GetBuffer后,必须使用ReleaseBuffer以更新对象内部数据<br> 例:csStr="abc";<br> LPTSTR pStr=csStr.GetBuffer(10);<br> strcpy(pStr,"12345");<br> cout<<csStr.GetLength(); <span style="font-size:14px; color:#990000; line-height:1.5em"> //3(错误的用法)</span><wbr><br> csStr.ReleaseBuffer();<br> cout<<csStr.GetLength(); <span style="font-size:14px; color:#990000; line-height:1.5em"> //5(正确)<br></span><wbr>pStr=NULL;<br><span style="font-size:14px; color:#990000; line-height:1.5em">//CString对象的任何方法都应在ReleaseBuffer之后调用</span><wbr><br><br><span style="font-size:14px; color:#0000cc; line-height:1.5em"><strong><wbr>LPTSTR GetBufferSetLength( int nNewLength );</wbr></strong><wbr></wbr></span><wbr><br> 申请新的空间,并返回指针<br> 例:csStr="abc";<br> csStr.GetBufferSetLength(20);<br> cout<<csStr;<span style="font-size:14px; color:#990000; line-height:1.5em">//abc<br></span><wbr>count<<csStr.GetLength();<span style="font-size:14px; color:#990000; line-height:1.5em"> //20;<br></span><wbr>csStr.ReleaseBuffer();<br> count<<csStr.GetLength(); <span style="font-size:14px; color:#990000; line-height:1.5em"> //3;<br> //使用GetBufferSetLength后可以不必使用ReleaseBuffer。<br></span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>