1 cstring Implementation Mechanism
Cstring manages strings through "reference", such as window kernel objects and COM objects. Cstring also uses this mechanism to manage allocated memory blocks. In fact, the cstring object only has one pointer member variable, so the length of any
The relationship and conversion between string, cstring, and char * have always been understandable, and the MFC program does not know when there will be one .. So I sorted out the online things based on my understanding.
I. Overview
FirstChar * (this is the basis)Because it should be the simplest and most basic, it is equivalent to a general int * or double * in terms of function, this basic structure. However, due to the special nature of the string
Reprinted:
Http://blog.pfan.cn/xman/43212.html
Getbuffer () is mainly used to lock the buffer length of the string, while releasebuffer is used to unlock the cstring object.CodeContinueAchieve adaptive length growth.
Cstring: getbuffer has two overloaded versions:
Lptstr getbuffer (); lptstr getbuffer (INT nminbufferlength );
In the second version, when the set length is smaller than the original
In the case of common ASCII encoding, the system stores cstring in a way similar to char * by default (in my opinion ).
For example, you can declare and assign a cstring as follows:
Char * charstr = "kenko ";
Cstring CSTR = charstr;
In ASCII encoding, cstring reads the memory location of the pointer behind it a
C ++ programming language is a widely used and powerful programming language. In the eyes of developers, this programming language plays an important role in program development. In C ++, The CString class is more powerful than the STL string class. when new users use CString, they will be attracted by its powerful functions. however, due to lack of knowledge about its internal mechanism, beginners are pron
In C, it indicates a string. It does not have the string concept. Only char * and char [] can be used. In MFC, to serve string operations, a class CString is added, the header file of this class is afx. h.The conversion from char * To CString is simple. You only need to use the CString constructor.This document describes how to convert
Cstring, Int, String, char * ConversionString to cstringCstring. Format ("% s", String. c_str ());Convert Char to cstringCstring. Format ("% s", char *);Char to stringString S (char *);String to char *Char * P = string. c_str ();Cstring to stringString S (cstring. getbuffer ());1, string-> cstringCstring. Format ("% s", String. c_str ());C_str () is indeed better
(1) convert char * To cstring
If char * is converted to cstring, you can use cstring: format in addition to direct value assignment. For example:
Char charray [] = "this is a test ";Char * P = "this is a test ";
Or
Lpstr P = "this is a test ";
Or in the use of Unicode applications that have been defined
Tchar * P = _ T ("this is a test ");
Or
Lptstr P
Summarize the various types of Unicode conversions that have recently been used, and encounter additional additions in the future:1. String Turn CStringString a= "abc";CString str=cstring (A.c_str ());Or Str.format ("%s", A.c_str ())2, int turn CStringInt A;CString Cstr;Cstr.format (_t ("%d"), a);3, Char turn CStringCstring.format ("%s", char*);Cases:Char szpath[
W_charis based on the negation of _unicode at compile time. Similarly, TCHAR is the same as the target Word macros.In the case of ANSI, LPCTSTR is a const char, a constant string (which cannot be modified), and LPTSTR is a char , an ordinary string (very large, modifiable).CString and LPCTSTRcstring and LPCTSTR can be said to be universal. The reason for this is the automatic type conversion defined by CString
1. Conversion of strings and numbers:Atof (string-> double, Int, long), ITOA (INT-> string), ltoa (long int-> string)Double-> cstring method: cstring: Format ("% d", dx );2. cstring to char *// After forced type conversion, the cstring type can be converted to char *, for example:Cstring CSTR = "Hello, world! ";Char *
It's been a long time. Finally, I have finished the problem of the conversion of the date format, and share my little experience here, hehe
The database I use is SQL Server2005, and there is a type of smalldatetime data in the database table to store the departure time (for example: 2010-1-1 10:00:00)
Request in VC + + to extract the time and compare with the current system time, Judge size
void Csaledlg::onbtnsurebuy (){Todo:add your control notification handler code hereUpdateData (TRUE);M_ed
To split a string based on multiple delimiters
SOURCE string
Seprator Separator
Number of substrings after count split
Return value: Split substring
cstring* Extractstr (LPCTSTR source, LPCTSTR seprator, int *count)
{
int isubstringcount,nindex=0;
CString strsource=source,strseperator=seprator,*pstrsubstrings;
if (Strseperator.getlength () ==0)
{
*count=0;
return NULL;
}
All the delimiters in
# Include
Using namespace STD; // same as above
# Include
# Include
# Include
// Because it is easier to write a conversion function, the function definition is as follows:
String getstring (const int N)
{
STD: stringstream newstr;
Newstr
Return newstr. STR ();
}
String to cstring
Cstring. Format ("% s", String. c_str ());
Convert Char to
1 cstring, Int, String, char * Conversion String to cstringCstring. Format ("% s", String. c_str ());Convert Char to cstringCstring. Format ("% s", char *);Char to stringString S (char *);String to char *Char * P = string. c_str ();Cstring to stringString S (cstring. getbuffer ());1, string-> cstringCstring. Format ("% s", String. c_str ());C_str () is indeed bet
This article Reprinted from: http://ygdljg.blog.163.com/blog/static/546010462008101191835269/
Http://www.vckbase.com/document/viewdoc? Id = 1096
Http://www.vckbase.com/document/viewdoc? Id = 1082
CW2A (BSTR)CW2A (_ bstr_t)CW2A (VARIANT. bstrVal)CW2A (_ varaint_t.bstrVal)
The macro W2T can be used.Void func (BSTR * lpsz){USES_CONVERSION;CString x = W2T (lpsz );}_ Variant_t var;// Convert all types to VT_BSTR: VariantChangeType ( var, var, 0, VT_BSTR
. Generally, after the conversion of lpctstr, it should only be used as a constant or as an input parameter of the function; and getbuffer (...) after getting the pointer, you can use this pointer to modify the content or make the input parameter of the function. Why? This code may often exist:Cstring STR ("ABCD ");Char * P = (char *) (const char *) STR;P [2] = 'Z ';In fact, there may be no errors in your program after such code, and the program runs well. But it is very dangerous. Check againCs
(char *);Only Initialization is allowed. It is best to use assign () instead of initialization ().
5. Convert string to char *Char * p = string. c_str ();
6. Convert CString to stringString s (CString. GetBuffer ());ReleaseBuffer () is required after GetBuffer (). Otherwise, no space occupied by the buffer is released.
7. Convert the content of a string to a character array and a C-string(1) data (), retur
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.