// 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"this
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endiform
//////////////////////////////////////////////////////////////////////
// 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;
}ip