MFC cstring usage summary 2

Source: Internet
Author: User
Tags uppercase character

(10) Search for strings in ListBox
Int Index =: sendmessage (m_stringlist.getsafehwnd (), lb_findstringexact,-1,
(Lparam) (lpctstr) strtext); // use the sendmessage function to send the lb_findstringexact message to the list control to check whether the specified string is in the list space. If yes, the index location is returned.

(11) String Array:
Cstring STR [5] array;
Cstring STR [5] = {"feiqiang", "mingri", "Mr "};
For (INT I = 0; I <5; I ++)
{
Array. Add (STR [I]); // Add an element
}
For (Int J = 0; j <array. Size (), J ++)
{// Character array size
If (array. Get (j) = "Mr ")
{
MessageBox ("exist ");
}

(12) set the encoding method: Project/settings à Preprocessor. If you want to use DBCS, add _ MBCS (multiple bytes). If you want to use Unicode, add _ Unicode, if this parameter is not added, ASCII is used.

Binary string pointer type

(1) lpcstr: 32-bit static string pointer, which can be directly assigned for use, for example, lpcstr STR = "mingrisofg ";

(2) lpstr: 32-Bit String pointer, such as lpstr STR; STR = new char [256];

(3) lpctstr: 32-bit Unicode static string pointer, for example, lpctstr STR = _ T ("mingrisoft ");

(4) lptstr: 32-bit Unicode string pointer, such as lptstr STR = new tchar [256];

Conversion between three BSTR (string type used for COM programming) and cstring:
1. When assigning values to BSTR variables:
BSTR = NULL;
BSTR = sysallocstring (L "feiqang"); // constructed from the lpcwstr
Sysfreestring (BSTR); // release
Forcibly convert BSTR to cstring, for example:
Cstring STR = (cstring) BSTR; or cstring STR; BSTR = Str. allocsysstring ();
2. _ BSTR _ (for the BSTR packaging class), including the header file: "comdef. H"
Usage:
Direct Value assignment: _ bstr_t tbstr = "feqiang ";
Assign a value to the cstring object: cstring STR = (lpcstr) tbstr; // lpcstr STR = tbstr;
Convert _ BSR _ to BSTR and use the copy function: BSTR = tbstr. Copy (); sysfreestring (BSTR );
BSTR is assigned to the _ BSTR _ object, for example, BSTR = sysallocstring (L "mingri"); _ bstr_t tbstr = BSTR;

Iv. Formatting type
For example, obtain and format the system time.
Ctime T = ctime: getcurrenttime ();
Cstring strtime = T. Format ("% H: % m: % s ");
MessageBox (strtime;
1. cstring: isempty
Bool isempty () const;
Return Value: if the length of the cstring object is 0, a non-zero value is returned; otherwise, 0 is returned.
Note: This member function is used to test whether a cstring object is null.
Example:
The following example shows how to use cstring: isempty.
// Cstring: isempty example
Cstring S;
Assert (S. isempty ());
See cstring: getlength

2. cstring: left
Cstring left (INT ncount) const;
Throw (cmemoryexception );
Return Value: The returned string is the first ncount character.
Example:
Cstring S (_ T ("abcdef "));
Assert (S. Left (2) = _ T ("AB "));

3. cstring: loadstring
Bool loadstring (uint NID );
Throw (cmemoryexception );
Return Value: If the resource is successfully loaded, a non-zero value is returned; otherwise, 0 is returned.
NID: A Windows string resource ID.
Note: This member function is used to read a Windows string resource identified by NID and put it into an existing cstring object.
Example:
The following example shows how to use cstring: loadstring.
// Cstring: loadstring example
# Define ids_filenotfound 1
Cstring S;
If (! S. loadstring (ids_filenotfound ))

4. cstring: makelower
Void makelower (); // change the lowercase character

5. cstring: makereverse
Void makereverse (); // character Inversion

6. cstring: makeupper
Void makeupper (); // change the uppercase character

7. cstring: Mid
Cstring mid (INT nfirst) const;
Cstring mid (INT nfirst, int ncount) const;
Ncount indicates the number of characters to be extracted, and nfirst indicates the start index location to be extracted.
Example:
Cstring S (_ T ("abcdef "));
Assert (S. mid (2, 3) = _ T ("CDE "));

8. cstring: releasebuffer
Void releasebuffer (INT nnewlength =-1 );
Parameter: nnewlength
The new length of this string, expressed by the number of characters, is not counted as an empty character at the end. If the word
If the string ends with an empty character, the default value-1 of the parameter sets the cstring size
The current length of the string.
Note:
Use releasebuffer to end the use of the buffer allocated by getbuffer. If you know
The nnewlength parameter can be omitted if the string in the punch area ends with a null character. If the character
If the string does not end with a null character, you can use nnewlength to specify the length of the string. Before calling
After releasebuffer or other cstring operations, the address returned by getbuffer is invalid.
Example:
The following example shows how to use cstring: releasebuffer.
// Cstring: releasebuffer example
Cstring S;
S = "ABC ";
Lptstr P = S. getbuffer (1024 );
Strcpy (P, "ABC ");//

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.