C++实现对汉字的完美支持

// TString.cpp: implementation of the CTString class.
//
//////////////////////////////////////////////////////////////////////
/**
 * <p>Title: CTSring 类</p>
 * <p>Description: 改写CString 的Left(),Right(),Mid(),GetLength()方法</p>
 * <p>Copyright: Copyright (c) 2011</p>
 * <p>QQ:372198960 </p>
 * @author 俊
 * @Date 2011-09-22
 * @version 1.0
 */
#include "stdafx.h"
#include "test11.h"
#include "TString.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CTString::CTString(CString sstr)
{
 this->str=sstr;
}
CTString::CTString()
{
 this->str="";
}
CTString::~CTString()
{
 
}
CString CTString::Mid(long nFirst)
{
 CTString tt=this->str;
 long len=tt.GetLength();
 return tt.Right(len-nFirst);
}
CString CTString::Mid(long nFirst,long nCount)
{
 CTString tt=this->str;
 long len=tt.GetLength();
 tt=tt.Right(len-nFirst);
 return tt.Left(nCount);
}
CString CTString::Right(long nCount)
{
 long size=this->GetLength();
 if(size==0 || nCount<=0){return "";}
 if(nCount>=size){return this->str;}
 char *m_firstBuffer=new char[1];
 char *m_secondBuffer=new char[1];
 LPSTR firstc=m_firstBuffer;
 LPSTR secondc=m_secondBuffer;
 char  TCbuf[1024];
 CString ret;
 long vi=size-nCount,itemp=0,i=0,j=0;
 while(itemp<vi)//确定i的位置
 {
  *firstc=this->str.GetAt(i);
  *secondc=this->str.GetAt(i+1);
  if((unsigned char)*firstc>=0xa1 && (unsigned char)*secondc>=0xa1) //两个字节都>=0xa1是汉字
  {
   i+=2;
  }else{
   i++;
  }
  itemp++;
 }
 vi=0;
 while(vi<nCount)
 {
  *firstc=this->str.GetAt(i);
  if(i+1<this->str.GetLength())
   *secondc=this->str.GetAt(i+1);
  else
   *secondc=0x00;  //如果越界了就赋个小于0xa1的值
  if((unsigned char)*firstc>=0xa1 && (unsigned char)*secondc>=0xa1) //两个字节都>=0xa1是汉字
  {  
   TCbuf[j]=*firstc;
   TCbuf[j+1]=*secondc;
   j+=2;
   i+=2;   
  }else{
   TCbuf[j]=*firstc;
   j++;
   i++;
  }
  vi++;
 }
 TCbuf[j]='\0';
 ret.Format("%s",TCbuf);
 return ret;
}
///左截取
CString CTString::Left(long nCount)
{
 long size=this->GetLength();
 if(size==0 || nCount<=0){return "";}
 if(nCount>=size){return this->str;}//如果截取的长度大于等于本身长度,则返回自身。
 char *m_firstBuffer=new char[1];
 char *m_secondBuffer=new char[1];
 LPSTR firstc=m_firstBuffer;
 LPSTR secondc=m_secondBuffer;
 char  TCbuf[1024];
 CString ret;
 long i=0,j=0,vi=0;
 //i 对单个字节的游标
 //j 存储截取的字节的游标
 //vi 截取长度的游标
 while(vi<nCount)
 {
  *firstc=this->str.GetAt(i);
  *secondc=this->str.GetAt(i+1);//左截取不会越界的。因为如果i到了最后一个的话说明截取的长度大于等于本身长度,那么之前就已经返回了。
  if((unsigned char)*firstc>=0xa1 && (unsigned char)*secondc>=0xa1) //汉字
  {
   TCbuf[j]=*firstc;
   TCbuf[j+1]=*secondc;
   j+=2;
   i+=2;
  }else{//非汉字
   TCbuf[j]=*firstc;
   j++;
   i++;
  }
  vi++;
 }
 TCbuf[j]='\0';
 ret.Format("%s",TCbuf);
 return ret;
}
/////获取字符串长度,汉字为一个字符
long CTString::GetLength()
{
long size=this->str.GetLength();
if(size==0) return 0;
char *m_firstBuffer=new char[1];
char *m_secondBuffer=new char[1];
LPSTR firstc=m_firstBuffer;
LPSTR secondc=m_secondBuffer;
long i=0;
long vlen=0;
while(i<size)
{
 *firstc=this->str.GetAt(i);
 if(i+1<this->str.GetLength())
  *secondc=this->str.GetAt(i+1);
 else
  *secondc=0x00;  //如果越界了就赋个小于0xa1的值
 if((unsigned char)*firstc>=0xa1 && (unsigned char)*firstc>=0xa1) //两个字节都是是汉字字节
 {
  i+=2;
  vlen++;
 }
 else
 {
  i+=1;
  vlen++;
 }
}
    return vlen;
}

你可能感兴趣的:(C++汉字乱码,C++操作汉字)