Using CString

1. Creating CString Objects from Standard C Literal Strings

    You can assign C-style literal strings to a CString just as you can assign one CString object to another.

     1.1)Assign the value of a C literal string to a CString object.

              CString myString = _T("This is a test");

     1.2)Assign the value of one CString to another CString object.

              CString oldString = _T("This is a test");
              CString newString = oldString;

           The contents of a CString object are copied when one CString object is assigned to another.

           Therefore, the two strings do not share a reference to the actual characters that make up the string.

    Note:To write your application so that it can be compiled for Unicode or for ANSI, code literal strings by using the _T macro.

2. Concatenating Two CString Objects

   To concatenate two CString objects, use the concatenation operators (+ or +=), as follows.

    CString s1 = _T("This ");        // Cascading concatenation
    s1 += _T("is a ");
    CString s2 = _T("test");
    CString message = s1 + _T("big ") + s2;  
    // Message contains "This is a big test".

   At least one argument to the concatenation operators (+ or +=) must be a CString object, but you can use a

   constant character string (for example, "big") or a char (for example, 'x') for the other argument.

3. Public Methods

Name

Description

CStringT::AllocSysString

Allocates a BSTR from CStringT data.

CStringT::AnsiToOem

Makes an in-place conversion from the ANSI character set to the OEM character set.

CStringT::AppendFormat

Appends formatted data to an existing CStringT object.

CStringT::Collate

Compares two strings (case sensitive, uses locale-specific information).

CStringT::CollateNoCase

Compares two strings (case insensitive, uses locale-specific information).

CStringT::Compare

Compares two strings (case sensitive).

CStringT::CompareNoCase

Compares two strings (case insensitive).

CStringT::Delete

Deletes a character or characters from a string.

CStringT::Find

Finds a character or substring inside a larger string.

CStringT::FindOneOf

Finds the first matching character from a set.

CStringT::Format

Formats the string as sprintf does.

CStringT::FormatMessage

Formats a message string.

CStringT::FormatMessageV

Formats a message string using a variable argument list.

CStringT::FormatV

Formats the string using a variable list of arguments.

CStringT::GetEnvironmentVariable

Sets the string to the value of the specified environment variable.

CStringT::Insert

Inserts a single character or a substring at the given index within the string.

CStringT::Left

Extracts the left part of a string.

CStringT::LoadString

Loads an existing CStringT object from a Windows resource.

CStringT::MakeLower

Converts all the characters in this string to lowercase characters.

CStringT::MakeReverse

Reverses the string.

CStringT::MakeUpper

Converts all the characters in this string to uppercase characters.

CStringT::Mid

Extracts the middle part of a string.

CStringT::OemToAnsi

Makes an in-place conversion from the OEM character set to the ANSI character set.

CStringT::Remove

Removes indicated characters from a string.

CStringT::Replace

Replaces indicated characters with other characters.

CStringT::ReverseFind

Finds a character inside a larger string; starts from the end.

CStringT::Right

Extracts the right part of a string.

CStringT::SetSysString

Sets an existing BSTR object with data from a CStringT object.

CStringT::SpanExcluding

Extracts characters from the string, starting with the first character, that are not in the set of characters identified by pszCharSet.

CStringT::SpanIncluding

Extracts a substring that contains only the characters in a set.

CStringT::Tokenize

Extracts specified tokens in a target string.

CStringT::Trim

Trims all leading and trailing whitespace characters from the string.

CStringT::TrimLeft

Trims leading whitespace characters from the string.

CStringT::TrimRight

Trims trailing whitespace characters from the string.

4. Operators

CStringT::operator =

Assigns a new value to a CStringT object.

CStringT::operator +

Concatenates two strings or a character and a string.

CStringT::operator +=

Concatenates a new string to the end of an existing string.

CStringT::operator ==

Determines if two strings are logically equal.

CStringT::operator !=

Determines if two strings are logically not equal.

CStringT::operator <

Determines if the string on the left side of the operator is less than to the string on the right side.

CStringT::operator >

Determines if the string on the left side of the operator is greater than to the string on the right side.

CStringT::operator <=

Determines if the string on the left side of the operator is less than or equal to the string on the right side.

CStringT::operator >=

Determines if the string on the left side of the operator is greater than or equal to the string on the right side.

 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------

(11)CStringT::Format

            Writes formatted data to a CStringT in the same way that sprintf_s formats data into a C-style character array.

            void __cdecl Format(
                   UINT nFormatID,
                   [, argument]...
             );
            void __cdecl Format(
                   PCXSTR pszFormat,
                   [, argument]...
            );

            Parameters

                nFormatID

                           The string resource identifier that contains the format-control string.

                pszFormat

                           A format-control string.

                argument

                           Optional arguments.

           Remarks

               This function formats and stores a series of characters and values in the CStringT. Each optional argument

                   (if any) is converted and output according to the corresponding format specification in pszFormat or from

                   the string resource identified by nFormatID.

              The call will fail if the string object itself is offered as a parameter to Format. For example, the following code

                  will cause unpredictable results:

                  CAtlString str = _T("Some Data");
                  str.Format(_T("%s%d"), str, 123);   
                  // Attention: str is also used in the parameter list.

           Example

                // typedef CStringT<TCHAR, StrTraitATL<TCHAR, ChTraitsCRT<TCHAR>>> CAtlString;
                CAtlString str;

                str.Format(_T("Floating point: %.2f\n"), 12345.12345);
                 _tprintf_s(_T("%s"), (LPCTSTR) str);

                str.Format(_T("Left-justified integer: %.6d\n"), 35);
                _tprintf_s(_T("%s"), (LPCTSTR) str);

   

 

 

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值