C++实现对汉字的完美支持
2019獨角獸企業重金招聘Python工程師標準>>>
// 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;
}
轉載于:https://my.oschina.net/u/208393/blog/35925
總結
以上是生活随笔為你收集整理的C++实现对汉字的完美支持的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 转载:DataTable使用技巧总结
- 下一篇: css 3d样子