学习CString方法的测试代码

// 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值