// yangyhui.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "afxwin.h"
int main(int argc, char* argv[])
{
CString str;
CString strTmp;
int i=1;
LPTSTR c;
c = "1234567890";
str = c;
for(;i<11;i++)
{
strTmp = str.Left(i);
printf("%s/n",strTmp);
}
printf("倒背如流!/n");
for(i=1;i<11;i++)
{
strTmp = str.Right(i);
printf("%s/n",strTmp);
}
printf("行云流水!/n");
for(i=0;i<9;i++)
{
str.Delete(0,1);
printf("%s/n",str);
}
int b;
b = str.Compare(strTmp);
printf("实践是真理的亲娘:%d/n",b);
str = strTmp;
b = str.Compare(strTmp);
printf("实践是真理的亲爹:%d/n",b);
b = str.Find("345",0);
printf("找啊找:%d/n",b);
TCHAR ch;
for(i=0;i<10;i++)
{
ch = str.GetAt(i);
printf("%d这个位置上是什么啊:%c/n",i,ch);
}
LPTSTR buff;
buff = str.GetBuffer(str.GetLength());
*(buff+5)='/0';
printf("%s/n",str);
str.ReleaseBuffer();
str += strTmp;
printf("%s/n",str);
i=str.Insert(5,"67890");
printf("插入成功了吗:i=%d/n",i);
printf("插入后的字符串:str=%s/n",str);
str = "ABcdEFg";
str.MakeLower();
printf("全是小写吗:str=%s/n",str);
str.MakeReverse();
printf("倒转了吗:str=%s/n",str);
str.MakeReverse();
printf("还原了吗:str=%s/n",str);
str.MakeUpper();
printf("全是大写吗:str=%s/n",str);
for(i=1;i<=6;i++)
{
strTmp = str.Mid(i);
printf("%s/n",strTmp);
}
printf("str=%s/n",str);
str = CString("aaaaaaaabcdefg");
i = str.Remove('a');
printf("str=%s/n",str);
str.TrimLeft("b");
str.TrimRight("g");
printf("str=%s/n",str);
return 0;
}
其中对于CString的GetBuff()方法MSDN上有一些关于对是否要在其后使用ReleaserBuff()方法做了如下的解释:
If you use the pointer returned by GetBuffer to change the string contents, you must call ReleaseBuffer before using any other CString member functions.
The address returned by GetBuffer may not be valid after the call to ReleaseBuffer since additional CString operations may cause the CString buffer to be reallocated. The buffer will not be reallocated if you do not change the length of the CString.
The buffer memory will be freed automatically when the CString object is destroyed.
Note that if you keep track of the string length yourself, you should not append the terminating null character. You must, however, specify the final string length when you release the buffer with ReleaseBuffer. If you do append a terminating null character, you should pass –1 for the length to ReleaseBuffer and ReleaseBuffer will perform a strlen on the buffer to determine its length.