zoukankan      html  css  js  c++  java
  • CString(转)

    CString::MakeUpper
    void MakeUpper( );
    Remarks
    备注Converts this CString object to an uppercase string.
    将原对象的所有小写英文字母转换为大写。(只是将小写的英文字母转换为大写,对于其它的字符不做变化,例如:大写字符,数字,汉字)
    Example实例
    The following example demonstrates the use of CString::MakeUpper.
    // example for CString::MakeUpper
    CString s( "abc" );
    s.MakeUpper();
    ASSERT( s == "ABC" );
    ---------------------------------------------------------------------------------------------------------------------------
    CString::MakeLower 
    void MakeLower( );
    Remarks备注
    Converts this CString object to a lowercase string.
    将原对象的所有大写英文字母转换为小写。(只是将大写的英文字母转换为小写,对于其它的字符不做变化,例如:小写字符,数字,汉字)
    Example实例
    The following example demonstrates the use of CString::MakeLower.
    // example for CString::MakeLower
    CString s( "ABC" );
    s.MakeLower();
    ASSERT( s == "abc" );
    ---------------------------------------------------------------------------------------------------------------------------
    CString::MakeReverse
    void MakeReverse( );
    Remarks备注
    Reverses the order of the characters in this CString object.
    将原对象的所有字符颠倒顺序。
    Example实例
    The following example demonstrates the use of CString::MakeReverse.
    // example for CString::MakeReverse
    CString s( "abc" );
    s.MakeReverse();
    ASSERT( s == "cba" );
    ---------------------------------------------------------------------------------------------------------------------------
    CString::Replace
    int Replace( TCHAR chOld, TCHAR chNew );
    int Replace( LPCTSTR lpszOld, LPCTSTR lpszNew );
    Return Value返回值
    The number of replaced instances of the character. Zero if the string isn't changed.
    该函数返回替换的字符数量。如果原对象没有改变则返回0。
    Parameters参数
    chOld
    The character to be replaced by chNew.
    将要被chNew所代替的字符。
    chNew
    The character replacing chOld.
    用来替换chOld的字符。
    lpszOld
    A pointer to a string containing the character to be replaced by lpszNew.
    lpszOld是一个指向字符串的指针,它所包含的字符将被lpszNew所代替。
    lpszNew
    A pointer to a string containing the character replacing lpszOld.
    lpszNew是一个指向字符串的指针,它所包含的字符用来替换lpszOld。
    Remarks备注
    Call this member function to replace a character with another. The first prototype of the function replaces instances of chOld with chNew in-place in the string. The second prototype of the function replaces instances of the substring lpszOld with instances of the string lpszNew.
    该函数用另外的字符来代替原来的字符。第一种形态,用chNew就地取代chOld。第二种形态,用lpszNew来取代原对象的子链lpszOld。
    The string may grow or shrink as a result of the replacement; that is, lpszNew and lpszOld do not have to be equal in length. Both versions perform case-sensitive matches.
    替换后的字符串有可能变长,也有可能缩短,也就是说,lpszNew和lpszOld的长度不必相等。两个形态都要区别大小写。
    Example实例
    //First example, with old and new equal in length.//第一个例子,长度相等的情况
    CString strZap("C--");
    int n = strZap.Replace('-', '+');
    ASSERT(n == 2);
    ASSERT(strZap == "C++");
    //Second example, old and new are of different lengths.//第二个例子,长度不相等的情况
    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 golf");
    (这里plays和golf之间是两个空格,如果NULL换成" ",那么就应该是3个空格)
    // note that you now have an extra space in your
    // sentence. To remove the extra space, include it 
    // in the string to be replaced, i.e.,"ice ".
    //注意句子中额外的空格。要消除它,那么被替换的字符串应该是"ice "。
    ---------------------------------------------------------------------------------------------------------------------------
    CString::Remove
    int CString::Remove( TCHAR ch );
    Return Value返回值
    The count of characters removed from the string. Zero if the string isn't changed.返回原对象中被清除的字符个数。如果原对象没有改变,则返回0。
    Parameters参数
    ch
    The character to be removed from a string.需要清除的字符。
    Remarks备注
    Call this member function to remove instances of ch from the string. Comparisons for the character are case-sensitive.该函数用来清除原对象中的字符ch。大小写不等效。
    Example实例
    //remove the lower-case letter 't' from a sentence://清除句子中的小写t
    CString str("This is a test.");
    int n = str.Remove('t');
    ASSERT(n == 2);
    ASSERT(str == "This is a es.");
    ---------------------------------------------------------------------------------------------------------------------------
    CString::Insert
    int Insert( int nIndex, TCHAR ch )
    throw( CMemoryException );
    int Insert( int nIndex, LPCTSTR pstr )
    throw( CMemoryException );
    Return Value返回值
    The length of the changed string.返回改变后的字符串长度。
    Parameters参数
    nIndex
    The index of the character before which the insertion will take place.用来确定插入的位置。
    ch
    The character to be inserted.需要插入的字符。
    pstr
    A pointer to the substring to be inserted.需要插入的子链的指针。
    Remarks备注
    Call this member function to insert a single character or a substring at the given index within the string. The nIndex parameter identifies the first character that will be moved to make room for the character or substring. If nIndex is zero, the insertion will occur before the entire string. If nIndex is higher than the length of the string, the function will concatenate the present string and the new material provided by either ch or pstr.
    该函数用来在原对象中的指定位置插入一个字符或子链。nIndex参数表示第一个为了给插入的字符或子链让位而被移动的字符。如果nIndex 为0,则在原对象的最前面插入。如果nIndex 大于了原对象的长度,该函数就将ch或者pstr连接到原函数的最后面。
    Example实例
    //The following example demonstrates the use of CString::Insert.
       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, '!');
       ASSERT(n == str.GetLength());
       printf("3: %s\n", (LPCTSTR) str);
    //this code generates these lines of output://以上代码产生如下的输出:
    1: Hockeyis Best
    2: Hockey is Best
    3: Hockey is Best!
    ---------------------------------------------------------------------------------------------------------------------------
    CString::Delete
    int Delete( int nIndex, int nCount = 1 )
    throw( CMemoryException );
    Return Value
    返回值
    The length of the changed string.返回改变后的字符串长度。
    Parameters参数
    nIndex
    The index of the first character to delete.表示第一个需要被删除的字符位置。
    nCount
    The number of characters to be removed.需要删除的字符个数。
    Remarks备注
    Call this member function to delete a character or characters from a string starting with the character at nIndex. If nCount is longer than the string, the remainder of the string will be removed.
    该函数用来删除原对象中从第nIndex+1个字符开始的nCount 个字符。如果nCount比字符串(应该是从第nIndex+1个字符开始的子链)的字符个数大,那么删除的就是从nIndex+1个字符开始的所有字符。
    Example实例
    //The following example demonstrates the use of CString::Delete.
       str2 = "Hockey is best!";
       printf("Before: %s\n", (LPCTSTR) str2);
       int n = str2.Delete(6, 3);
       printf("After: %s\n", (LPCTSTR) str2);
       ASSERT(n == str2.GetLength());
    //this code generates this line of output:
    Before: Hockey is best!
    After: Hockey best!
    ---------------------------------------------------------------------------------------------------------------------------
    CString::Format
    void Format( LPCTSTR lpszFormat, ... );
    void Format( UINT nFormatID, ... );
    Parameters参数
    lpszFormat
    A format-control string.格式控制字符串。
    nFormatID
    The string resource identifier that contains the format-control string.包含格式控制字符串的字符串资源标记。
    Remarks备注
    Call this member function to write formatted data to a CString in the same way that sprintf formats data into a C-style character array. This function formats and stores a series of characters and values in the CString. Each optional argument (if any) is converted and output according to the corresponding format specification in lpszFormat or from the string resource identified by nFormatID.
    该函数将数据格式化为CString对象,其用法和使用sprintf函数将数据格式化为C语言风格的字符数组一样。该函数将一连串的字符和数值格式化并 存放到CString对象中。某变量(如果有)被转换,并且按照lpszFormat或者字符串资源标记nFormatID规定的格式输出。
    The call will fail if the string object itself is offered as a parameter to Format. For example, the following code:
    如果CString对象本身被当作参数提供给Format,那么函数会调用失败。例如下面的代码:
    CString str = "Some Data";
    str.Format("%s%d", str, 123);
    // Attention: str is also used in the parameter list.//注意:str也被用作参数
    will cause unpredictable results.将导致不可预知的结果。
    When you pass a character string as an optional argument, you must cast it explicitly as LPCTSTR. The format has the same form and function as the format argument for the printf function. (For a description of the format and arguments, see printf in the Run-Time Library Reference.) A null character is appended to the end of the characters written.
    当把字符串当作参数传递的时候,必须象LPCTSTR一样明确地声明它。其格式和功能与printf的形参一样(关于格式和参数的说明,参阅Run-Time Library Reference中的sprintf函数)。写入字符的末端没有字符被添加。
    For more information, see sprintf in the Run-Time Library Reference.
    更多说明参阅Run-Time Library Reference(运行库参考手册)中的sprintf函数。
    Example实例
    CString str;
    str.Format(_T("Floating point: %.2f\n"), 12345.12345);
    _tprintf("%s", (LPCTSTR) str);
    str.Format(_T("Left-justified integer: %.6d\n"), 35);
    _tprintf("%s", (LPCTSTR) str);
    str.Format(IDS_SCORE, 5, 3);
    _tprintf("%s", (LPCTSTR) str);
    Output输出
    If the application has a string resource with the identifier IDS_SCORE that contains the string "Penguins: %d\nFlyers : %d\n", the above code fragment produces this output:
    如果使用包含字符串"Penguins: %d\nFlyers : %d\n"的字符串资源标识符IDS_SCORE,则上面的代码将产生如下输出:
    Floating point: 12345.12
    Left-justified integer: 000035
    Penguins: 5
    Flyers : 3
    CString::FormatV
    void FormatV( LPCTSTR lpszFormat, va_list argList );
    Parameters参数
    lpszFormat
    A format-control string.格式控制字符串。
    argList
    A list of arguments to be passed.
    被传递的一列参数。
    Remarks备注
    Call this member function to write a formatted string and a variable list of arguments to a CString object in the same way that vsprintf formats data into a C-style character array. This function formats and stores a series of characters and values in the CString. The string and arguments are converted and output according to the corresponding format specification in lpszFormat.
    该函数返回一个具有一定格式和一个参数表的CString对象(?),就象vsprintf函数将数据格式化为C风格的字符数组一样。该函数格式化并储存一列字符和数值在CString中。字符串和参数按指定的格式格式化并输出。
    The call will fail if the string object itself is offered as a parameter to FormatV. For example, the following code:
    如果CString对象本身被当作参数提供给FormatV,那么函数会调用失败。例如下面的代码:
    CString str = "Some Data";
    str.FormatV("%s%d", str, 123);    
    // Attention: str is also used in the parameter list.//注意:str也被用作参数
    will cause unpredictable results.将导致不可预知的结果。
    For more information, see vsprintf in the Run-Time Library Reference.更多说明参阅Run-Time Library Reference(运行库参考手册)中的vsprintf函数。
    Example实例
    //Using CString::FormatV(), you can write functions like the following:
    void WriteLogEntry(CStdioFile& refFile, LPCTSTR pstrFormat, ...)
    {
       CTime timeWrite;
       timeWrite = CTime::GetCurrentTime();
     
       // write the time out
       CString str = timeWrite.Format("%d %b %y %H:%M:%S - ");
       refFile.Write(str, str.GetLength());
     
       // format and write the data we were given
       va_list args;
       va_start(args, pstrFormat);
       str.FormatV(pstrFormat, args);
       refFile.Write(str, str.GetLength());
     
       // put a newline
       refFile.Write("\n", 1);
       return;
    }
    You can call the above function with any number of parameters, for example:
     
        WriteLogEntry(fileLog, "Program started");
       WriteLogEntry(fileLog, "Processed %d bytes", 91341);
       WriteLogEntry(fileLog, "%d error(s) found in %d line(s)", 10, 1351);
       WriteLogEntry(fileLog, "Program completed");
    which would add output to your fileLog file similar to the following:
     
        17 Apr 97 12:34:53 - Program started
       17 Apr 97 12:34:59 - Processed 91341 bytes
       17 Apr 97 12:35:22 - 10 error(s) found in 1351 line(s)
       17 Apr 97 12:35:23 - Program completed
    ---------------------------------------------------------------------------------------------------------------------------
    CString::TrimLeft
    void TrimLeft( );
    void CString::TrimLeft( TCHAR chTarget );
    void CString::TrimLeft( LPCTSTR lpszTargets );
    Parameters参数
    chTarget
    The target characters to be trimmed.需要被清除的字符。
    lpszTargets
    A pointer to a string containing the target characters to be trimmed.含有需要被清除字符的字符串指针。
    Remarks备注
    Call the version of this member function with no parameters to trim leading whitespace characters from the string. When used with no parameters, TrimLeft removes newline, space, and tab characters.
    该函数没有参数的形式用来清除原对象前面的whitespace(百度了一下,whitespace 表示空格、制表符、回车等特殊字符,后面的说明正好证明了其含义)。不含参数的TrimLeft清除换行,空格,制表符。
    Use the versions of this function that accept parameters to remove a particular character or a particular group of characters from the beginning of a string.
    含有参数的该函数用来清除原对象前面的指定的字符或字符集。
    For more information, see Strings Topics in Visual C++ Programmer's Guide
    更多说明参阅更加深入的编程话题(VC++程序员指南?)中的Strings Topics。
    Example实例
    In this example, the string "\t\t   ****Hockey is best!" becomes "Hockey is best!":
    下面的例子,字符串 "\t\t   ****Hockey is best!" 变成了 "Hockey is best!":
    CString strBefore;
    CString strAfter;
     
    strBefore = _T("\t\t    ****Hockey is best!");
    strAfter = strBefore;
    strAfter.TrimLeft(T_("\t *"));
     
    _tprintf(_T("Before: \"%s\"\n"), (LPCTSTR) strBefore);
    _tprintf(_T("After : \"%s\"\n"), (LPCTSTR) strAfter);
    MSDN这里完全没说清楚,自己补充(通过测试自己总结的,不一定对):
        没有参数的形式已经说清楚了,无须补充;有参数的两种形式,前一种可以看作后一种的特例;
        接受字符串指针lpszTargets作为参数的形式,从原对象中找出第一个没有在lpszTargets中出现的字符,将此字符及其之后的部分用来更新 原对象。若原对象的第一个字符没有在lpszTargets中出现,则原对象不变;若原对象的所有字符都在lpszTargets中出现,则原对象为空。
        该形式和CString::SpanIncluding类似,都是从原对象中找出第一个没有在参数中出现的字符,只是前者用后面部分更新原对象,而后者是返回前面部分。
    ---------------------------------------------------------------------------------------------------------------------------
    CString::TrimRight
    void TrimRight( );
    void CString::TrimRight( TCHAR chTarget );
    void CString::TrimRight( LPCTSTR lpszTargets );
    Parameters
    chTarget
    The target characters to be trimmed.需要被清除的字符。
    lpszTargets
    A pointer to a string containing the target characters to be trimmed.含有需要被清除字符的字符串指针。
    Remarks备注
    Call the version of this member function with no parameters to trim trailing whitespace characters from the string. When used with no parameters, TrimRight removes trailing newline, space, and tab characters from the string.
    该函数没有参数的形式用来清除原对象后面的whitespace。不含参数的TrimRight清除换行,空格,制表符。
    Use the versions of this function that accept parameters to remove a particular character or a particular group of characters from the end of a string.含有参数的该函数用来清除原对象后面的指定的字符或字符集。
    实际上就是从原对象最后面开始,向前找出第一个没有在lpszTargets所指向的字符串中出现的字符,将该字符及其前面的部分用来更新原对象。更新后原对象有可能为空,也有可能不变。
    Example实例
    CString strBefore;
    CString strAfter;
     
       strBefore = "Hockey is Best!!!!";
       strAfter = strBefore;
       str.TrimRight('!');
       printf("Before: \"%s\"\n", (LPCTSTR) strBefore);
       printf("After : \"%s\"\n\n", (LPCTSTR) strAfter);
     
       strBefore = "Hockey is Best?!?!?!?!";
       strAfter = strBefore;
       str.TrimRight("?!");
       printf("Before: \"%s\"\n", (LPCTSTR) strBefore);
       printf("After : \"%s\"\n\n", (LPCTSTR) strAfter);
    In the first example above, the string reading, "Hockey is Best!!!!" becomes "Hockey is Best".
    上面的第一个例子,原对象由"Hockey is Best!!!!" 变成了 "Hockey is Best"。
    In the second example above, the string reading, , "Hockey is Best?!?!?!?!" becomes "Hockey is Best".
    上面的第二个例子,原对象由"Hockey is Best?!?!?!?!" 变成了 "Hockey is Best"。
    For more information, see Strings Topics in Visual C++ Programmer's Guide
    更多说明参阅更加深入的编程话题中的Strings Topics。
    ---------------------------------------------------------------------------------------------------------------------------
    CString::FormatMessage
    void FormatMessage( LPCTSTR lpszFormat, ... );
    void FormatMessage( UINT nFormatID, ... );
    Parameters参数
    lpszFormat
    Points to the format-control string. It will be scanned for inserts and formatted accordingly. The format string is similar to run-time function printf-style format strings, except it allows for the parameters to be inserted in an arbitrary order.
    格式控制字符串指针。其作用是确定插入的字符及其格式,除了允许参数可以按照任意的顺序插入外,和(运行函数)printf的格式相同。
    nFormatID
    The string resource identifier that contains the unformatted message text.
    包含无格式的消息正文的字符串资源标识符(?)。
    Remarks备注
    Call this member function to format a message string. The function requires a message definition as input. The message definition is determined by lpszFormat or from the string resource identified by nFormatID. The function copies the formatted message text to the CString, processing any embedded insert sequences if requested.
    该函数格式化消息串,将lpszFormat 或nFormatID确定的消息作为输入,然后复制消息正文到CString。若需要,该函数按顺序处理插入项。
    Each insert must have a corresponding parameter following the lpszFormat or nFormatID parameter. Within the message text, several escape sequences are supported for dynamically formatting the message. For a description of these escape sequences and their meanings, see the Windows ::FormatMessage function in the Win32 SDK Programmer's Reference.
    在 lpszFormat或nFormatID后面,每个插入项必须有相应的参数。在消息正文中,动态格式化支持一些escape sequences(换码顺序?)。
    Example实例
    CString str;
    int nAsked = 5;
    int nAgree = 4;
    str.FormatMessage(_T("%1!d! of %2!d! developers agree: Hockey is %3%!"), 
       nAgree, nAsked, _T("Best"));
    ASSERT(str == _T("4 of 5 developers agree: Hockey is Best!"));
    ---------------------------------------------------------------------------------------------------------------------------
    CString::Compare
    int Compare( LPCTSTR lpsz ) const;
    返回值   字符串一样 返回0
     
             小于lpsz 返回-1
     
             大于lpsz 返回1
     
             区分大小字符
     
             CString s1( "abc" );
     
    CString s2( "abd" );
     
    ASSERT( s1.Compare( s2 ) == -1 );
     
    ASSERT( s1.Compare( "abe" ) == -1 );
     
     
     
    CString::CompareNoCase
     
    int CompareNoCase( LPCTSTR lpsz ) const;
     
    返回值 字符串一样 返回0
     
            小于lpsz 返回-1
     
            大于lpsz 返回1
     
            不区分大小字符
     
     
     
    CString::Collate
     
    int Collate( LPCTSTR lpsz ) const;
     
    同CString::Compare
     
     
     
    CString::CollateNoCase
     
    int CollateNocase( LPCTSTR lpsz ) const;
     
    同CString::CompareNoCase
     
     
     
    CString::CString
     
    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";
     
     
     
    CString::Delete
     
    int Delete( int nIndex, int nCount = 1);
     
    返回值是被删除前的字符串的长度
     
    nIndex是第一个被删除的字符,nCount是一次删除几个字符。根据我实验得出的结果:当nCount>要删除字符串的最大长度(GetCount() - nIndex)时会出错,当nCount过大,没有足够的字符删除时,此函数不执行。
     
    例子
     
    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)就会执行错误
     
     
     
    CString::Empty
     
    Void Empty( );
     
    没有返回值 清空操作;
     
    例子
     
    CString s( "abc" );
     
    s.Empty();
     
    ASSERT( s.GetLength( ) == 0 );
     
     
     
    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)
     
     
     
    CString::FindOneOf
     
    int FindOneOf( LPCTSTR lpszCharSet ) const;
     
    返回值 不匹配的话返回 -1; 索引以0 开始
     
              注意::返回此字符串中第一个在lpszCharSet中 也包括字符并且从零开始的索引值
     
    例子
     
    CString s( "abcdef" );
     
    ASSERT( s.FindOneOf( "xd" ) == 3 ); // 'd' is first match.
     
     
     
    CString::Format
     
    void Format( LPCTSTR lpszFormat, ... );
     
    void Format( UINT nFormatID, ... );
     
    lpszFormat 一个格式控制字符串
     
    nFormatID 字符串标识符
     
    例子
     
                 CString str;
     
    Str.Format(“%d”,13);
     
    此时Str为13
     
     
     
    CString::GetAt
     
    TCHAR GetAt( int nIndex ) const;
     
    返回标号为nIndex的字符,你可以把字符串理解为一个数组,GetAt类似于[].注意nIndex的范围,如果不合适会有调试错误。
     
     
     
    CString::GetBuffer
     
    LPTSTR GetBuffer( int nMinBufLength );
     
    返回值
     
    一个指向对象的(以空字符结尾的)字符缓冲区的LPTSTR 指针。
     
    参数
     
    nMinBufLength
     
    字符缓冲区的以字符数表示的最小容量。这个值不包括一个结尾的空字符的空间。
     
    说明
     
    此成员函数返回一个指向CString 对象的内部字符缓冲区的指针。返回的LPTSTR 不是const,因此可以允许直接修改CString 的内容。如果你使用由GetBuffer 返回的指针来改变字符串的内容,你必须在使用其它的CString 成员函数之前调用ReleaseBuffer 函数。
     
    在调用ReleaseBuffer 之后,由GetBuffer 返回的地址也许就无效了,因为其它的CString 操作可能会导致CString 缓冲区被重新分配。如果你没有改变此CString 的长度,则缓冲区不会被重新分配。当此CString 对象被销毁时,其缓冲区内存将被自动释放。
     
    注意,如果你自己知道字符串的长度,则你不应该添加结尾的空字符。但是,当你用 ReleaseBuffer 来释放该缓冲区时,你必须指定最后的字符串长度。如果你添加了结尾的空字符, 你应该给ReleaseBuffer 的长度参数传递-1 ,ReleaseBuffer 将对该缓冲区执行strlen 来确定它的长度。
     
    下面的例子说明了如何用CString::GetBuffer。
     
    // 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
     
     
     
    CString::GetLength
     
    int GetLength( ) const;
     
    返回值
     
    返回字符串中的字节计数。
     
    说明
     
    此成员函数用来获取这个CString 对象中的字节计数。这个计数不包括结尾的空字符。
     
    对于多字节字符集(MBCS),GetLength 按每一个8 位字符计数;即,在一个多字节字符中的开始和结尾字节被算作两个字节。
     
    示例
     
    下面的例子说明了如何使用CString::GetLength。
     
    // CString::GetLength 示例
     
    CString s( "abcdef" );
     
    ASSERT( s.GetLength() == 6 );
     
     
     
    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!
     
     
     
    CString::IsEmpty
     
    BOOL IsEmpty( ) const;
     
    返回值
     
    如果CString 对象的长度为0,则返回非零值;否则返回0。
     
    说明
     
    此成员函数用来测试一个CString 对象是否是空的。
     
    示例
     
    下面的例子说明了如何使用CString::IsEmpty。
     
    // CString::IsEmpty 示例
     
    CString s;
     
    ASSERT( s.IsEmpty() );
     
    请参阅 CString::GetLength
     
     
     
    CString::Left
     
    CString Left( int nCount ) const;
     
    throw( CMemoryException );
     
    返回的字符串是前nCount个字符。
     
    例子
     
    CString s( _T("abcdef") );
     
    ASSERT( s.Left(2) == _T("ab") );
     
     
     
    CString::LoadString
     
    BOOL LoadString( UINT nID );
     
    throw( CMemoryException );
     
    返回值
     
    如果加载资源成功则返回非零值;否则返回0。
     
    nID 一个Windows 字符串资源ID。
     
    说明 此成员函数用来读取一个由nID 标识的Windows 字符串资源,并放入一个已有CString 对象中。
     
    示例
     
    下面的例子说明了如何使用CString::LoadString。
     
    // CString::LoadString 示例
     
    #define IDS_FILENOTFOUND 1
     
    CString s;
     
    if (! s.LoadString( IDS_FILENOTFOUND ))
     
     
     
    CString::MakeLower
     
    void MakeLower( );
     
    改变字符的小写
     
     
     
    CString::MakeReverse
     
    void MakeReverse( );
     
    字符倒置
     
     
     
    CString::MakeUpper
     
    void MakeUpper( );
     
    改变字符的大写
     
    CString::Mid
     
    CString Mid( int nFirst ) const;
     
    CString Mid( int nFirst, int nCount ) const;
     
    nCount代表要提取的字符数, nFirst代表要提取的开始索引位置
     
    例子
     
    CString s( _T("abcdef") );
     
    ASSERT( s.Mid( 2, 3 ) == _T("cde") );
     
    CString::ReleaseBuffer
     
    void ReleaseBuffer( int nNewLength = -1 );
     
    参数
     
    nNewLength
     
    此字符串的以字符数表示的新长度,不计算结尾的空字符。如果这个字
     
    符串是以空字符结尾的,则参数的缺省值-1 将把CString 的大小设置为
     
    字符串的当前长度。
     
    说明
     
    使用ReleaseBuffer 来结束对由GetBuffer 分配的缓冲区的使用。如果你知道缓
     
    冲区中的字符串是以空字符结尾的,则可以省略nNewLength 参数。如果字符
     
    串不是以空字符结尾的,则可以使用nNewLength 指定字符串的长度。在调用
     
    ReleaseBuffer 或其它CString 操作之后,由GetBuffer 返回的地址是无效的。
     
    示例
     
    下面的例子说明了如何使用CString::ReleaseBuffer。
     
    // CString::ReleaseBuffer 示例
     
    CString s;
     
    s = "abc";
     
    LPTSTR p = s.GetBuffer( 1024 );
     
    strcpy(p, "abc"); // 直接使用该缓冲区
     
    ASSERT( s.GetLength() == 3 ); // 字符串长度 = 3
     
    s.ReleaseBuffer(); // 释放多余的内存,现在p 无效。
     
    ASSERT( s.GetLength() == 3 ); // 长度仍然是3
     
     
     
    CString::Remove
     
    int CString::Remove ( TCHAR ch );
     
    返回值
     
    返回从字符串中移走的字符数。如果字符串没有改变则返回零。
     
    参数
     
    ch
     
    要从一个字符串中移走的字符。
     
    说明
     
    此成员函数用来将ch 实例从字符串中移走。与这个字符的比较是区分大小写
     
    的。
     
    示例
     
    // 从一个句子中移走小写字母'c':
     
    CString str (“This is a test.”);
     
    int n = str.Remove( 't' );
     
    ASSERT( n == 2 );
     
    ASSERT( str ==“This is a es. ” );
     
     
     
    CString::Replace
     
    int Replace( TCHAR chOld, TCHAR chNew );
     
    int Replace( LPCTSTR lpszOld, LPCTSTR lpszNew );
     
    返回值
     
    返回被替换的字符数。如果这个字符串没有改变则返回零。
     
    参数
     
    chOld
     
    要被chNew 替换的字符。
     
    chNew
     
    要用来替换chOld 的字符。
     
    lpszOld
     
    一个指向字符串的指针,该字符串包含了要被lpszNew 替换的字符。
     
    lpszNew
     
    一个指向字符串的指针,该字符串包含了要用来替换lpszOld 的字符。
     
    说明
     
    此成员函数用一个字符替换另一个字符。函数的第一个原形在字符串中用chNew
     
    现场替换chOld。函数的第二个原形用lpszNew 指定的字符串替换lpszOld 指定
     
    的子串。
     
    在替换之后,该字符串有可能增长或缩短;那是因为lpszNew 和lpszOld 的长度
     
    不需要是相等的。两种版本形式都进行区分大小写的匹配。
     
    示例
     
    // 第一个例子,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” );
     
    // 注意,现在在你的句子中有了一个额外的空格。
     
    // 要移走这个额外的空格,可以将它包括在要被替换的字符串中,例如,“ice ”。
     
     
     
    CString::ReverseFind
     
    int ReverseFind( TCHAR ch ) const;
     
    返回值
     
    返回此CString 对象中与要求的字符匹配的最后一个字符的索引;如果没有找
     
    到需要的字符则返回-1。
     
    参数
     
    ch
     
    要搜索的字符。
     
    说明
     
    此成员函数在此CString 对象中搜索与一个子串匹配的最后一个字符。此函数
     
    类似于运行时函数strrchr。
     
    示例
     
    // CString::ReverseFind 示例
     
    CString s( "abcabc" );
     
    ASSERT( s.ReverseFind( 'b' ) == 4 );
     
     
     
    CString::Right
     
    CString Right( int nCount ) const;
     
    throw( CMemoryException );
     
    返回的字符串是最后nCount个字符。
     
    CString s( _T("abcdef") );
     
    ASSERT( s.Right(2) == _T("ef") );
     
     
     
    CString:: SetAt
     
    void SetAt( int nIndex, TCHAR ch );
     
     
     
    你可以把字符串理解为一个数组,SetAt类似于[].注意nIndex的范围,如果不合适会有调试错误。 Ch 更替字符, 把nIndex位置上的字符 变成ch
     
     
     
    CString s( "abc" );
     
    s.MakeReverse();
     
    ASSERT( s == "cba" );
     
     
     
     
     
    CString::TrimLeft
     
    void TrimLeft( );
     
    void CString::TrimLeft( TCHAR chTarget );
     
    如果没有参数,从左删除字符(\n\t空格等),至到遇到一个非此类字符. 当然你也可以指定删除那些字符. 如果指定的参数是字符串,那么遇上其中的一个字符就删除.
     
    \n 换行符
     
    \t TAB字符
     
    CString str = "\n\t a";
     
    str.TrimLeft();
     
    str为“a”;
     
     
     
    CString str = "abbcadbabcadb ";
     
    str.TrimLeft("ab");
     
    结果"cadbabcadb "
     
    str.TrimLeft("ac");
     
    结果"bcadbabcadb "
     
     
     
    CString::TrimRight
     
    void TrimRight( );
     
    void CString::TrimRight( TCHAR chTarget );
  • 相关阅读:
    [LeetCode] Bulb Switcher II 灯泡开关之二
    [LeetCode] Second Minimum Node In a Binary Tree 二叉树中第二小的结点
    [LeetCode] 670. Maximum Swap 最大置换
    [LeetCode] Trim a Binary Search Tree 修剪一棵二叉搜索树
    [LeetCode] Beautiful Arrangement II 优美排列之二
    [LeetCode] Path Sum IV 二叉树的路径和之四
    [LeetCode] Non-decreasing Array 非递减数列
    [LeetCode] 663. Equal Tree Partition 划分等价树
    [LeetCode] 662. Maximum Width of Binary Tree 二叉树的最大宽度
    [LeetCode] Image Smoother 图片平滑器
  • 原文地址:https://www.cnblogs.com/bigbigtree/p/2253650.html
Copyright © 2011-2022 走看看