cstring->std::string Example:
CString strmfc= "Test";
Std::string Strstl;
Strstl=strmfc.getbuffer (0);
Unicode Scenario:
CStringW strw = _t ("test");
CStringA Stra (STRW. GetBuffer (0));
STRW. ReleaseBuffer ();
Std::string Imgpath=stra. GetBuffer (0);
Stra. ReleaseBuffer ();
std::string->cstring Example:
CString Strmfc;
std::string strstl= "Test";
Strmfc=strstl.c_str ();
Afxextractsubstring is a function of intercepting a string, very useful, but in the ointment where it can only use a single character as a separator.
But this situation does not work in many cases, if the separator needs to be two characters or more?
I've been searching the internet for a long time because of this problem. But unfortunately, most of the internet on the VC intercept string of the article are so the same few, are written full of complex and can achieve the afxextractsubstring function only, that is, can only be intercepted with a single character, but the title is written with a string to intercept the string, funny!
Don't find it, write it yourself. The CString contains find, and then the array is formed.
void Split (CString source, cstringarray& Dest, CString Division)
{
Dest. RemoveAll ();
int pos = 0;
int pre_pos = 0;
while (-1! = pos) {
Pre_pos = pos;
pos = source. Find (Division, (pos+1));
Dest. ADD (source. Mid (Pre_pos, (Pos-pre_pos)));
}
}
CString source is the original string that needs to be intercepted,
cstringarray& Dest is an array of final results
CString Division is a string used to make a separator
Memo: In order to apply to the Unicode environment, develop the habit of using _t () macros
1. Formatting strings
CString s;
S.format (_t ("The NUM is%d."), I);
2. Convert to int
Turn 10 binary is best used _ttoi (), which is compiled into _atoi () in the ANSI encoding system and compiled into _wtoi () in the Unicode encoding system. Use _tcstoul () or _tcstol () to convert a string into any arbitrary (unsigned/signed) long Integer.
CString hex = _t ("FAB");
CString decimal = _t ("4011");
ASSERT (_tcstoul (hex, 0, +) = = _ttoi (decimal));
3. Convert to char *
3.1 Coercion type conversion to LPCTSTR, cannot modify string
LPCTSTR p = s; or direct (LPCTSTR) s;
3.2 Using the GetBuffer method
It uses the default value of 0 when not passing parameters to GetBuffer, meaning: "Give me a pointer to this string, I promise not to grow it." Assuming that you want to increase the length of the string, you must put the size of the character space you need (note: It is a character rather than a byte, because CString is implicitly aware of Unicode) passed to it. When ReleaseBuffer is called, the actual length of the string is recalculated and then stored in the CString object.
It must be emphasized that in this range between GetBuffer and ReleaseBuffer, you must not use any method of this buffered CString object that you want to manipulate. Because ReleaseBuffer is called, the integrity of the CString object is not guaranteed.
LPTSTR p = s.getbuffer ();
Do something with P
int m = S.getlength (); Possible Error!!!
S.releasebuffer ();
int n = s.getlength (); Ensure correct
4. Other
4.1 Split string
Afxextractsubstring (cstring& rString, LPCTSTR lpszfullstring, int isubstring, TCHAR chsep = '/n ');
CString csfullstring (_t ("ABCD-EFG-HIJK-LMN"));
CString cstemp;
Afxextractsubstring (Cstemp, (LPCTSTR) csfullstring, 0, '-'); Get ABCD
Afxextractsubstring (Cstemp, (LPCTSTR) csfullstring, 1, '-'); Get EFG
Afxextractsubstring (Cstemp, (LPCTSTR) csfullstring, 2, '-'); Get Hijk
Afxextractsubstring (Cstemp, (LPCTSTR) csfullstring, 3, '-'); Get LMN
The delimiter can be arbitrarily specified:
Afxextractsubstring (Cstemp, (LPCTSTR) csfullstring, 0, ' f '); Get ABCD-E
http://blog.csdn.net/yysdsyl/article/details/2463662
C + + implements CString and string conversions