vc中最主要函数不易理解。

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;

}

1、Collate，Compare

2、Delete

int Delete( int nIndex, int nCount = 1 )

3、FindOneOf

int FindOneOf( LPCTSTR lpszCharSet ) const;

CString str = "0123456789";

int x = str.FindOneOf("31");

x的值是1。

4、Find

int Find( TCHAR ch ) const;

int Find( LPCTSTR lpszSub ) const;

int Find( TCHAR ch, int nStart ) const;

int Find( LPCTSTR pstr, int nStart ) const;

CString str = "0123456789";

int x = str.Find("34",4);

5、GetAt

TCHAR GetAt( int nIndex ) const;

6、Insert

int Insert( int nIndex, TCHAR ch )
int Insert( int nIndex, LPCTSTR pstr
)

7、Left

CString Left( int nCount ) const;

Right与Left类似

8、MakeLower ，MakeUpper改变字符的大小写

MakeReverse字符倒置，如：

CString str = "X0123456789";

str.MakeReverse();

str变为"9876543210X"

9、+=

const CString& operator +=( const CString& string );
const CString& operator +=( TCHAR ch );
const CString& operator +=( LPCTSTR lpsz );

str+="ha";

str为"0123456789ha"；

10、str[]

TCHAR operator []( int nIndex ) const;

CString str = "0123456789";

str[0]='x';

11、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 lpszTargets );

CString str = "\n\t a";

str.TrimLeft();

str为“a”;

如果没有参数,从左删除字符(\n\t空格等),至到遇到一个非此类字符.

str.TrimLeft("ab");

12、int CString::Remove( TCHAR ch );

ch删除的字符.

二、CString 与char []之间的转换.

char str[100] = ”str”;

CString sstr = “sstr”;

str.Format(“%s”,str);

str = LPCTSTR sstr;

strcpy(str,(LPCTSTR)sstr);

CString s(_T("This is a test "));
LPTSTR p = s.GetBuffer();
// 在这里添加使用p的代码
if(p != NULL) *p = _T('\0');
s.ReleaseBuffer();
// 使用完后及时释放，以便能使用其它的CString成员函数

str的值变了.

三、将NULL字节放入CString中
1,CString str("abc\0""def", 7);
str +="g";
int nLength = str.GetLength();
nLength为8.
2,CString str("My name is hedan!");
str.SetAt(5, 0);
int nLength = str.GetLength();

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);