## CSDN博客

### CString

http://blog.csdn.net/he_zhidan/archive/2004/02/11/16999.aspx

CString

1,主要函数的实现

2,常用函数

3.CStringchar []的相互转换

4,将NULL字节放入CString中

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;

}

CollateCompare       与一个字符长指针所指的字符串进行比较，与strcmp相同，它们的区别是，分别调用_tcscoll_tcsicmp

Delete

int Delete( int nIndex, int nCount = 1 )

FindOneOf

int FindOneOf( LPCTSTR lpszCharSet ) const;

CString str = "0123456789";

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

x的值是1

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

GetAt

TCHAR GetAt( int nIndex ) const;

Insert

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

Left

CString Left( int nCount ) const;

RightLeft类似

MakeLower MakeUpper改变字符的大小写

MakeReverse字符倒置，如：

CString str = "X0123456789";

str.MakeReverse();

str变为"9876543210X"

+=

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

str+="ha";

str"0123456789ha"

str[]

TCHAR operator []( int nIndex ) const;

CString str = "0123456789";

str[0]='x';

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

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的值变了.

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

0 0