• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            VC:CString用法整理(轉(zhuǎn)載)

            1.CString::IsEmpty

            BOOL IsEmpty( ) const;

            返回值:如果CString 對象的長度為0,則返回非零值;否則返回0。

            說明:此成員函數(shù)用來測試一個CString 對象是否是空的。

            示例:

            下面的例子說明了如何使用CString::IsEmpty。

            // CString::IsEmpty 示例

            CString s;

            ASSERT( s.IsEmpty() );

            請參閱 CString::GetLength

             

            2.CString::Left

            CString Left( int nCount ) const; 

            throw( CMemoryException );

            返回值:返回的字符串是前nCount個字符。

            示例:

            CString s( _T("abcdef") );

            ASSERT( s.Left(2) == _T("ab") );

             

            3.CString::LoadString

            BOOL LoadString( UINT nID );

            throw( CMemoryException );

            返回值:如果加載資源成功則返回非零值;否則返回0。

            nID  一個Windows 字符串資源ID。

            說明: 此成員函數(shù)用來讀取一個由nID 標(biāo)識的Windows 字符串資源,并放入一個已有CString 對象中。

            示例:

            下面的例子說明了如何使用CString::LoadString。

            // CString::LoadString 示例

            #define IDS_FILENOTFOUND 1

            CString s;

            if (! s.LoadString( IDS_FILENOTFOUND ))

             

            4.CString::MakeLower

            void MakeLower( );  //改變字符的小寫

             

            5.CString::MakeReverse

            void MakeReverse( );  //字符倒置

             

            6.CString::MakeUpper

            void MakeUpper( );  //改變字符的大寫

             

            7.CString::Mid CString Mid( int nFirst ) const; CString Mid( int nFirst, int nCount ) const; nCount代表要提取的字符數(shù), nFirst代表要提取的開始索引位置

            示例: CString s( _T("abcdef") ); ASSERT( s.Mid( 2, 3 ) == _T("cde") );

             

            8.CString::ReleaseBuffer

            void ReleaseBuffer( int nNewLength = -1 );

            參數(shù):nNewLength

            此字符串的以字符數(shù)表示的新長度,不計算結(jié)尾的空字符。如果這個字

            符串是以空字符結(jié)尾的,則參數(shù)的缺省值-1 將把CString 的大小設(shè)置為

            字符串的當(dāng)前長度。

            說明:

            使用ReleaseBuffer 來結(jié)束對由GetBuffer 分配的緩沖區(qū)的使用。如果你知道緩

            沖區(qū)中的字符串是以空字符結(jié)尾的,則可以省略nNewLength 參數(shù)。如果字符

            串不是以空字符結(jié)尾的,則可以使用nNewLength 指定字符串的長度。在調(diào)用

            ReleaseBuffer 或其它CString 操作之后,由GetBuffer 返回的地址是無效的。

            示例:

            下面的例子說明了如何使用CString::ReleaseBuffer。

            // CString::ReleaseBuffer 示例

            CString s;

            s = "abc";

            LPTSTR p = s.GetBuffer( 1024 );

            strcpy(p, "abc"); // 直接使用該緩沖區(qū)

            ASSERT( s.GetLength() == 3 ); // 字符串長度 = 3

            s.ReleaseBuffer(); // 釋放多余的內(nèi)存,現(xiàn)在p 無效。

            ASSERT( s.GetLength() == 3 ); // 長度仍然是3

             

            9.CString::Remove

            int CString::Remove ( TCHAR ch );

            返回值:返回從字符串中移走的字符數(shù)。如果字符串沒有改變則返回零。

            參數(shù):ch  要從一個字符串中移走的字符。

            說明:此成員函數(shù)用來將ch 實例從字符串中移走。與這個字符的比較是區(qū)分大小寫

            的。

            示例:

            // 從一個句子中移走小寫字母'c':

            CString str (“This is a test.”);

            int n = str.Remove( 't' );

            ASSERT( n == 2 );

            ASSERT( str ==“This is a es. ” );

             10.CString::Replace

            int Replace( TCHAR chOld, TCHAR chNew );

            int Replace( LPCTSTR lpszOld, LPCTSTR lpszNew );

            返回值:返回被替換的字符數(shù)。如果這個字符串沒有改變則返回零。

            參數(shù):chOld     要被chNew 替換的字符。

            chNew    要用來替換chOld 的字符。

            lpszOld   一個指向字符串的指針,該字符串包含了要被lpszNew 替換的字符。

            LpszNew  一個指向字符串的指針,該字符串包含了要用來替換lpszOld 的字符。

            說明:此成員函數(shù)用一個字符替換另一個字符。函數(shù)的第一個原形在字符串中用chNew

            現(xiàn)場替換chOld。函數(shù)的第二個原形用lpszNew 指定的字符串替換lpszOld 指定

            的子串。

            在替換之后,該字符串有可能增長或縮短;那是因為lpszNew 和lpszOld 的長度

            不需要是相等的。兩種版本形式都進(jìn)行區(qū)分大小寫的匹配。

            示例:

            // 第一個例子,old 和new 具有相同的長度。

            CString strZap( “C - -” );

            int n = strZap.Replace('-', '+' );

            ASSERT( n == 2 );

            ASSERT(strZap == “C++” );

            // 第二個例子,old 和new 具有不同的長度。

            CString strBang( “Everybody likes ice hockey” );

            n = strBang.Replace( “hockey”, “golf” );

            ASSERT( n ==1 );

            n = strBang.Replace ( “likes” , “plays” );

            ASSERT( n == 1 );

            n = strBang.Replace( “ice”, NULL );

            ASSERT( n == 1 );

            ASSERT( strBang == “Everybody plays golg” );

            // 注意,現(xiàn)在在你的句子中有了一個額外的空格。

            // 要移走這個額外的空格,可以將它包括在要被替換的字符串中,例如,“ice ”。

             

            11.CString::ReverseFind

            int ReverseFind( TCHAR ch ) const;

            返回值: 返回此CString 對象中與要求的字符匹配的最后一個字符的索引;如果沒有找

            到需要的字符則返回-1。

            參數(shù): ch  要搜索的字符。

            說明:此成員函數(shù)在此CString 對象中搜索與一個子串匹配的最后一個字符。此函數(shù)

            類似于運行時函數(shù)strrchr。

            示例:

            // CString::ReverseFind 示例

            CString s( "abcabc" );

            ASSERT( s.ReverseFind( 'b' ) == 4 );

             

            12.CString::Right

            CString Right( int nCount ) const; 

            throw( CMemoryException );

            返回值: 返回的字符串是最后nCount個字符。

            CString s( _T("abcdef") );

            ASSERT( s.Right(2) == _T("ef") );

             

            13.CString:: SetAt

            void SetAt( int nIndex, TCHAR ch );

             說明:可以把字符串理解為一個數(shù)組,SetAt類似于[].注意nIndex的范圍,如果不合適會有調(diào)試錯誤。 Ch 更替字符, 把nIndex位置上的字符 變成ch

             示例:

            CString s( "abc" );

            s.MakeReverse();

            ASSERT( s == "cba" );

             

            14.CString::TrimLeft

            void TrimLeft( );

            void CString::TrimLeft( TCHAR chTarget );

            說明:如果沒有參數(shù),從左刪除字符(\n\t空格等),至到遇到一個非此類字符. 當(dāng)然你也可以指定刪除那些字符. 如果指定的參數(shù)是字符串,那么遇上其中的一個字符就刪除. \n  換行符 \t  TAB字符

            示例1: CString str = "\n\t a";

            str.TrimLeft();

            str為“a”;

             示例2:

            CString str = "abbcadbabcadb ";

            str.TrimLeft("ab");

            結(jié)果"cadbabcadb "

            str.TrimLeft("ac");

            結(jié)果"bcadbabcadb "

             

            15.CString::TrimRight

            void TrimRight( );

            void CString::TrimRight( TCHAR chTarget );

            void CString::TrimRight( LPCTSTR lpszTargets );

            說明:用法類似于上面。

             

            16.CString::Compare

            int Compare( LPCTSTR lpsz ) const;

            返回值:字符串一樣返回0,小于lpsz  返回-1,大于lpsz  返回1, 區(qū)分大小字符

            示例:

            CString s1( "abc" );

            CString s2( "abd" );

            ASSERT( s1.Compare( s2 ) == -1 );

            ASSERT( s1.Compare( "abe" ) == -1 

            17.CString::CompareNoCase

            int CompareNoCase( LPCTSTR lpsz ) const;

            返回值: 字符串一樣 返回0,小于lpsz  返回-1,大于lpsz  返回1,不區(qū)分大小字符

             

            18.CString::Collate

            int Collate( LPCTSTR lpsz ) const;

            同CString::Compare

             

            19.CString::CollateNoCase

            int CollateNocase( LPCTSTR lpsz ) const;

            同CString::CompareNoCase

             

            20.CString::CString      //構(gòu)造函數(shù)

            CString( );

            CString( const CString& stringSrc ); 

            CString( TCHAR ch, int nRepeat = 1 ); 

            CString( LPCTSTR lpch, int nLength );

            CString( const unsigned char* psz ); 

            CString( LPCWSTR lpsz );

            CString( LPCSTR lpsz );

            示例:

            CString s1;                     

            CString s2( "cat" );              

            CString s3 = s2;                 

            CString s4( s2 + " " + s3 );        

            CString s5( 'x' );                      // s5 = "x"

            CString s6( 'x', 6 );                   // s6 = "xxxxxx"

            CString s7((LPCSTR)ID_FILE_NEW);        // s7 = "Create a new document"

            CString city = "Philadelphia";

             

            21.CString::Delete

            int Delete( int nIndex, int nCount = 1); 

            返回值:是被刪除前的字符串的長度

            nIndex是第一個被刪除的字符,nCount是一次刪除幾個字符。根據(jù)我實驗得出的結(jié)果:當(dāng)nCount>要刪除字符串的最大長度(GetCount() - nIndex)時會出錯,當(dāng)nCount過大,沒有足夠的字符刪除時,此函數(shù)不執(zhí)行。

            示例:

            CString str1,str2,str3;

            char a;

            str1 = "nihao";

            str2 = "nIhao";

            int x;

            // int i=(str1 == str2);      

            str1.Delete(2,3);

            如果nCount(3) > GetCount() – nIndex (5-2)就會執(zhí)行錯誤

             

            22.CString::Empty

            Void Empty( );

            返回值:沒有返回值  清空操作;

            示例:

            CString s( "abc" );

            s.Empty();

            ASSERT( s.GetLength( ) == 0 );

             

            23.CString::Find

            int Find( TCHAR ch ) const;

            int Find( LPCTSTR lpszSub ) const;

            int Find( TCHAR ch, int nStart ) const;

            int Find( LPCTSTR lpszSub, int nStart ) const;

            返回值:  不匹配的話返回 -1;  索引以0 開始; nStar 代表以索引值nStart 的字符開始搜索 ,

            即為包含以索引nStart字符后的字符串.

            示例:

            CString s( "abcdef" );

            ASSERT( s.Find( 'c' ) == 2 );

            ASSERT( s.Find( "de" ) == 3 );

            Cstring str(“The stars are aligned”);

            Ing n = str.Find('e',5);

            ASSERT(n == 12)

             

            24.CString::FindOneOf

            int FindOneOf( LPCTSTR lpszCharSet ) const;

            返回值:  不匹配的話返回 -1;  索引以0 開始

            注意::返回此字符串中第一個在lpszCharSet中也包括字符并且從零開始的索引值

            示例:

            CString s( "abcdef" );

            ASSERT( s.FindOneOf( "xd" ) == 3 ); // 'd' is first match.

             

            25.CString::Format

            void Format( LPCTSTR lpszFormat, ... );

            void Format( UINT nFormatID, ... );

            參數(shù):lpszFormat  一個格式控制字符串

            nFormatID  字符串標(biāo)識符

            示例:

            CString str;

            Str.Format(“%d”,13); 

            此時Str為13

             

            26.CString::GetAt

            TCHAR GetAt( int nIndex ) const;

            返回值:返回標(biāo)號為nIndex的字符,你可以把字符串理解為一個數(shù)組,GetAt類似于[].注意nIndex的范圍,如果不合適會有調(diào)試錯誤。

             

            27.CString::GetBuffer

            LPTSTR GetBuffer( int nMinBufLength );

            返回值:一個指向?qū)ο蟮模ㄒ钥兆址Y(jié)尾的)字符緩沖區(qū)的LPTSTR 指針。

            參數(shù):nMinBufLength

            字符緩沖區(qū)的以字符數(shù)表示的最小容量。這個值不包括一個結(jié)尾的空字符的空間。

            說明:此成員函數(shù)返回一個指向CString 對象的內(nèi)部字符緩沖區(qū)的指針。返回的LPTSTR 不是const,因此可以允許直接修改CString 的內(nèi)容。如果你使用由GetBuffer 返回的指針來改變字符串的內(nèi)容,你必須在使用其它的CString 成員函數(shù)之前調(diào)用ReleaseBuffer 函數(shù)。

            在調(diào)用ReleaseBuffer 之后,由GetBuffer 返回的地址也許就無效了,因為其它的CString 操作可能會導(dǎo)致CString 緩沖區(qū)被重新分配。如果你沒有改變此CString 的長度,則緩沖區(qū)不會被重新分配。當(dāng)此CString 對象被銷毀時,其緩沖區(qū)內(nèi)存將被自動釋放。

            注意:如果你自己知道字符串的長度,則你不應(yīng)該添加結(jié)尾的空字符。但是,當(dāng)你用ReleaseBuffer 來釋放該緩沖區(qū)時,你必須指定最后的字符串長度。如果你添加了結(jié)尾的空字符,你應(yīng)該給ReleaseBuffer 的長度參數(shù)傳遞-1 ,ReleaseBuffer 將對該緩沖區(qū)執(zhí)行strlen 來確定它的長度。

            示例:

            // CString::GetBuffer 例子

            CString s( "abcd" );

            #ifdef _DEBUG

            afxDump << "CString s " << s << "\n";

            #endif

            LPTSTR p = s.GetBuffer( 10 );

            strcpy( p, "Hello" ); // 直接訪問CString 對象。

            s.ReleaseBuffer( );

            #ifdef _DEBUG

            afxDump << "CString s " << s << "\n";

            #endif

             

            28.CString::GetLength

            int GetLength( ) const;

            返回值:返回字符串中的字節(jié)計數(shù)。

            說明:此成員函數(shù)用來獲取這個CString 對象中的字節(jié)計數(shù)。這個計數(shù)不包括結(jié)尾的空字符。

            對于多字節(jié)字符集(MBCS),GetLength 按每一個8 位字符計數(shù);即,在一個多字節(jié)字符中的開始和結(jié)尾字節(jié)被算作兩個字節(jié)。

            示例

            下面的例子說明了如何使用CString::GetLength。

            // CString::GetLength 示例

            CString s( "abcdef" );

            ASSERT( s.GetLength() == 6 );

             

            29.CString::Insert

            int Insert( int nIndex, TCHAR ch ); 

            int Insert( int nIndex, LPCTSTR pstr );

            返回值:返回修改后的長度,nIndex是字符(或字符串)插入后的索引號例子

            示例:

            CString str( “HockeyBest”);

             int n = str.Insert( 6, “is” );

             ASSERT( n == str.GetLength( ) ); 

            printf( “1: %s\n”, ( LPCTSTR ) str );

             n = str.Insert( 6, ' ' ); 

            ASSERT( n == str.GetLength( ) ); 

            printf ( “2: %s\n”, (LPCTSTR) STR );

             n = str.Insert(555, ‘1’);

            ASSERT( n == str.GetLength ( ) );

             printf ( “3: %s\n”, ( LPCTSTR ) str );

            輸出

            1. Hockeyis Best

            2. Hockey is Best

            3. Hockey is Best!

            posted on 2009-05-22 09:19 wrh 閱讀(48194) 評論(0)  編輯 收藏 引用


            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            導(dǎo)航

            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            統(tǒng)計

            常用鏈接

            留言簿(19)

            隨筆檔案

            文章檔案

            收藏夾

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久被窝电影亚洲爽爽爽| 亚洲日本久久久午夜精品| 久久国产精品国语对白| 狼狼综合久久久久综合网| 欧洲性大片xxxxx久久久| 女人香蕉久久**毛片精品| 老色鬼久久亚洲AV综合| 亚洲综合伊人久久大杳蕉| 久久久久久久久久久| 四虎国产精品成人免费久久| 国产精品亚洲综合专区片高清久久久 | 久久99精品九九九久久婷婷| 青青青青久久精品国产h| 久久最新精品国产| 久久久精品国产Sm最大网站| 久久夜色精品国产www| 欧美久久亚洲精品| 久久精品国产亚洲AV影院| 久久久久久夜精品精品免费啦 | 久久99精品久久久久久9蜜桃| 久久九色综合九色99伊人| 久久影院亚洲一区| 久久精品国产亚洲av麻豆蜜芽| 性欧美大战久久久久久久久| 久久国产精品一国产精品金尊| 久久99精品久久久久久| 久久久久亚洲AV无码专区网站| 97精品伊人久久大香线蕉| 成人久久精品一区二区三区| 国产精品无码久久久久| 久久久久波多野结衣高潮| 久久99精品国产麻豆宅宅| 一本久道久久综合狠狠躁AV| 久久精品亚洲一区二区三区浴池 | 九九久久精品国产| 人妻精品久久久久中文字幕一冢本| 成人妇女免费播放久久久| 国产午夜精品理论片久久| 九九久久自然熟的香蕉图片| 久久本道久久综合伊人| 色偷偷久久一区二区三区|