VC entry 3 (String)

zhaozj2021-02-16  50

Cstring

He Zhidan

main content:

1, the implementation of the main function

2, common functions

3. CSTRING and CHAR [] Mutual conversion

4, put the null byte into the cstring

The most important function in VC is not easy to understand.

CString :: cstring (char * p)

{

INT N = Strlen (P);

m_data = new char [n 1];

STRCPY (M_Data, P);

}

CString :: CString (CString & Other)

{

INT N = Strlen (Other.m_data);

m_data = new char [n 1];

STRCPY (m_data, other.m_data);

}

CSTRING & CSTRING :: Operator = (CString & Other)

{

delete [] m_data;

INT N = Strlen (Other.m_data);

m_data = new char [n 1];

STRCPY (m_data, other.m_data);

Return * this;

}

String :: ~ String ()

{

delete [] m_data;

}

COLLATE, Compare compares the string referred to by a character long pointer, the same as strCMP, the difference is, call _tcscoll, _tcsicmp.

Delete

Int delete (int nindex, int ncount = 1)

The return value is the length of the string before being deleted, NINDEX is the first deleted character, and Ncount is a delete several characters. According to my experiment: When the length of the NCOUNT> string is wrong, this function is not executed when Ncount is too large. When there is not enough character deletion, this function is not executed.

FINDONEOF

INT FINDONEOF (LPCTSTR LPSZCHARSET) Const;

The function of this function is to find any of the characters in the lpszcharset, and then return the position, and do not find the return 0. Such as:

CString str = "0123456789";

INT x = str.findoneof ("31");

The value of x is 1.

Find

INT FIND (TCHAR CH) Const;

INT FIND (LPCTSTR LPSZSUB) Const;

INT FIND (TCHAR CH, INT NSTART) Const;

INT FIND (LPCTSTSTR PSTR, INT NSTART) Const;

Return the value to the serial number, the character to be searched, the character string of lpszsub to be searched, NSTART starts searching. Such as:

CString str = "0123456789";

INT x = str.find ("34", 4);

The returned value is -1.

Getat

Tchar getat (int NINDEX) Const;

Returns the character of the NINDEX, you can understand the string as an array, Getat is similar to []. Note Nindex range, if not suitable will have debugging errors.

Insert

INT INSERT (INT NINDEX, TCHAR CH) INT INSERT (INT NINDEX, LPCTSTSTR PSTR) Returns the modified length, NINDEX characters (or string) inserted into the label.

Left

CSTRING LEFT (int Ncount) const; returned to the front NCOUNT character.

Right similar to Left

Makelower, makeupper changes the case of characters

MakeReverse characters inverted, such as:

CSTRING STR = "x0123456789";

Str.makereverse ();

STR becomes "9876543210x"

=

Const CSTRING & OPERATOR = (const cstring & string); const cstring & operator = (tchar ch); const cstring & operator = (lpctstr lps);

Combine the parameters to yourself.

Such as: cstring str = "0123456789";

Str = "ha";

STR is "0123456789ha";

Str []

TCHAR OPERATOR [] (int NINDEX) Const;

Treat strings like processing character arrays.

Note that it is read-only.

CString str = "0123456789";

STR [0] = 'x';

it's wrong.

Trimleft, Trimright

Void Trimleft ();

Void Cstring :: Trimleft (tchar chtarget);

Void cstring :: Trimleft (LPCTSTR LPSZTARGETS);

Void trimright ();

Void Cstring :: Trimright (tchar chtarget);

Void cstring :: Trimright (lpctstr lpsztarget);

CString str = "/ n / t a";

Str.trimleft ();

STR is "a";

If there is no parameters, the characters (/ N / T space, etc.) are removed from the left, until there is a non-such character.

Of course, you can also specify deleting those characters.

If the specified parameter is a string, then one of the characters will be deleted.

CString str = "abbcadbabcadb";

Str.trimLEFT ("ab");

Result "CadbabCADB"

INT CSTRING :: Remove (tchar ch);

CH Deleted characters.

Returns the number of deleted characters, there are multiple times to delete.

Conversion between CString and CHAR [].

Char Str [100] = "STR";

CString sstr = "sstr";

Str.Format ("% s", str);

Str = LPCTSTR SSTR;

STRCPY (STR, (LPCTSTR) SSTR);

If it is assigned, you want:

CSTRING S ("this is a test"); lptstr p = s.getBuffer (); // Add the code IF (P! = Null) * p = _t ('/ 0') here S.ReleaseBuffer (); // Release after using it to use other CString member functions

The value of the STR changed.

Put a null byte into CString 1, CString STR ("ABC / 0" "DEF", 7); Str = "G"; int NLENGTH = Str.getLength (); NLENGTH is 8.2, CString Str ("My Name is hedan! "); str.sett (5, 0); int NLENGTH = Str.getLength (); Note: Not all CString member functions can, be careful when using it. Example: dynamic configuration data source CString strDsn, strDBQ; strDsn = "DSN = test"; strDBQ.Format ( "DBQ =% s", strSourceMDBName); CString strConnect = strDsn "" strDBQ; strConnect.SetAt (strDsn.GetLength (), '/ 0'); SQLConfigDataSource (NULL, ODBC_ADD_SYS_DSN, "Microsoft Access Driver (* .mdb)", StrConnect);

转载请注明原文地址:https://www.9cbs.com/read-25727.html

New Post(0)