分享
 
 
 

适合于Unix与Win32下的字符串处理类,可以以此为基类进行扩展

王朝vc·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

/****************************CString.h******************************/

#ifndef MyStringH

#define MyStringH

#ifndef WIN32

typedef int bool;

#define false 0

#define true !false

#endif

class CString{

public:

CString();

CString(const char *str);

CString(CString &str);

virtual ~CString();

const char * c_str()const;

CString& sprintf(const char* format, ...);

int Length() const;

CString Trim() const;

CString TrimLeft() const;

CString TrimRight() const;

CString UpperCase() const;

CString LowerCase() const;

CString StringOfChar(char ch, int count)const;//StringOfChar('A',4)returns "AAAA"

CString operator+(const CString &str); file://Not return reference

CString operator +(const char* lhs);

CString &operator +=(const CString& rhs);

CString &operator=(const CString &str);

char &operator [](const int idx);

bool operator !=(const CString& rhs) const;

bool operator <(const CString& rhs) const;

bool operator <=(const CString& rhs) const;

bool operator ==(const CString& rhs) const;

bool operator >(const CString& rhs) const;

bool operator >=(const CString& rhs) const;

file://返回被分隔符分割的字符串,如CString("ab|cd")::GetDelimitedString("|",1)返回"cd"

CString GetDelimitedString(const char *delimiter,int index);

private:

char cTmp;//when operator[] over range,use this var

char *_data;//store the data

};

#endif

/****************************CString.cpp******************************/

file://---------------------------------------------------------------------------

#include "CString.h"

#include <string.h>

#include <stdarg.h>

#include <stdio.h>

file://---------------------------------------------------------------------------

CString::CString():_data(0)

{

_data=new char[1];

_data[0]='\0';

}

file://-------------------------------------------------------------------

CString::CString(const char *str):_data(0)

{

if(str)

{

_data=new char[strlen(str)+1];

file://memset(_data,0x00,strlen(str)+1);

strcpy(_data,str);

}

else

{

_data=new char[1];

_data[0]='\0';

}

}

file://--------------------------------------------------------------------

CString::CString(CString &str):_data(0)

{

file://必须在拷贝函数中为动态分配空间的指针分配空间

_data=new char[str.Length()+1];

strcpy(_data,str._data);

}

file://----------------------------------------------------------------------

CString::~CString()

{

delete []_data;

}

file://---------------------------------------------------------------------

file://return the length of this string,not counting the null-terminating character.

int CString::Length() const

{

return strlen(_data);

}

file://-----------------------------------------------------------------------

CString & CString::operator=(const CString &str)

{

if(this==&str) file://can't be "this==str"

{

return *this;

}

delete []_data;//for the previous 'new'

_data=new char[str.Length()+1];

strcpy(_data,str._data);

return *this;

}

file://------------------------------------------------------------------------

file://the value returned by c_str points to the internal character

file://array referenced by the _data property

const char * CString::c_str()const

{

return _data;

}

file://----------------------------------------------------------------------

CString& CString::sprintf(const char* format, ...)

{

#ifdef WIN32

delete []_data;

va_list paramList;

va_start(paramList, format);

int size = vsnprintf(NULL, 0, format, paramList);

_data=new char[size+1];

memset(_data,0x00,size+1);

vsnprintf(_data, size, format, paramList);//Only for WIN32

va_end(paramList);

return *this;

#else

delete []_data;

_data=new char[1024*5];//maxsize:5k,may cause overflow

va_list paramList;

va_start(paramList, format);

vsprintf(_data,format, paramList);

va_end(paramList);

return *this;

#endif

}

file://--------------------------------------------------------------------

CString CString::operator+(const CString &str)

{

CString tmp;

tmp=*this;

delete []_data;

_data=new char[tmp.Length()+str.Length()+1];

memset(_data,0x00,tmp.Length()+str.Length()+1);

strcpy(_data,tmp.c_str());

strcat(_data,str.c_str());

return *this;

}

file://-----------------------------------------------------------------------

CString CString::operator +(const char* str)

{

CString tmp;

tmp=*this;

delete []_data;

_data=new char[tmp.Length()+strlen(str)+1];

memset(_data,0x00,tmp.Length()+strlen(str)+1);

strcpy(_data,tmp.c_str());

strcat(_data,str);

return *this;

}

file://-----------------------------------------------------------------------

CString &CString::operator +=(const CString& rhs)

{

CString tmp;

tmp=*this;

delete []_data;

_data=new char[tmp.Length()+rhs.Length()+1];

memset(_data,0x00,tmp.Length()+rhs.Length()+1);

strcpy(_data,tmp.c_str());

strcat(_data,rhs.c_str());

return *this;

}

file://------------------------------------------------------------------------

char &CString::operator [](const int idx)

{

if( (idx<0) || (idx>Length()-1) )

return cTmp;//if over range,return the reference of cTmp

else

return _data[idx];

}

file://----------------------------------------------------------------------

bool CString::operator !=(const CString& rhs) const

{

if( strcmp(_data,rhs.c_str())!=0)

return true;

else

return false;

}

file://--------------------------------------------------------------------------

bool CString::operator <(const CString& rhs) const

{

if( strcmp(_data,rhs.c_str())<0)

return true;

else

return false;

}

file://----------------------------------------------------------------------

bool CString::operator <=(const CString& rhs) const

{

if( strcmp(_data,rhs.c_str())<=0)

return true;

else

return false;

}

file://-----------------------------------------------------------------------

bool CString::operator ==(const CString& rhs) const

{

if( strcmp(_data,rhs.c_str())==0)

return true;

else

return false;

}

file://--------------------------------------------------------------------

bool CString::operator >(const CString& rhs) const

{

if( strcmp(_data,rhs.c_str())>0)

return true;

else

return false;

}

file://----------------------------------------------------------------------

bool CString::operator >=(const CString& rhs) const

{

if( strcmp(_data,rhs.c_str())>=0)

return true;

else

return false;

}

file://-----------------------------------------------------------------------

CString CString::TrimLeft() const

{

CString Result;

char *pDest,*pTmp;

pTmp=new char[Length()+1];

memset(pTmp,0x00,Length()+1);

strcpy(pTmp,_data);

pDest=pTmp;

for(int i=0;i<Length();i++)

{

if( (*pDest==' ') || (*pDest=='\n') || (*pDest=='\t') )

pDest++;

else

break;

}

Result=pDest;

delete []pTmp;

return Result;

}

file://-----------------------------------------------------------------------

CString CString::TrimRight() const

{

CString Result;

char *pDest,*pTmp;

pTmp=new char[Length()+1];

memset(pTmp,0x00,Length()+1);

strcpy(pTmp,_data);

pDest=pTmp+Length()-1;

for(int i=Length();i>0;i--)

{

if( (*pDest==' ') || (*pDest=='\n') || (*pDest=='\t') )

pDest--;

else

break;

}

*(++pDest)=0;//Cut to here

Result=pTmp;

delete []pTmp;

return Result;

}

file://-----------------------------------------------------------------------

CString CString::Trim() const

{

CString Result;

char *pDest,*pTmp,*p;

pTmp=new char[Length()+1];

memset(pTmp,0x00,Length()+1);

strcpy(pTmp,_data);

pDest=pTmp;

for(int i=0;i<Length();i++)

{

if( (*pDest==' ') || (*pDest=='\n') || (*pDest=='\t') )

pDest++;

else

break;

}

p=pTmp+Length()-1;

for(int j=Length();j>0;j--)

{

if( (*p==' ') || (*p=='\n') || (*p=='\t') )

p--;

else

break;

}

*(++p)=0;//Cut to here

Result=pDest;

delete []pTmp;

return Result;

}

file://-----------------------------------------------------------------------

CString CString::UpperCase() const

{

CString Result;

char *pTmp;

pTmp=new char[Length()+1];

memset(pTmp,0x00,Length()+1);

strcpy(pTmp,_data);

for(int i=0;i<Length();i++)

{

if( (pTmp[i]>='a') && (pTmp[i]<='z') )

{

pTmp[i]-=('a'-'A');

}

}

Result=pTmp;

delete []pTmp;

return Result;

}

file://-----------------------------------------------------------------------

CString CString::LowerCase() const

{

CString Result;

char *pTmp;

pTmp=new char[Length()+1];

memset(pTmp,0x00,Length()+1);

strcpy(pTmp,_data);

for(int i=0;i<Length();i++)

{

if( (pTmp[i]>='A') && (pTmp[i]<='Z') )

{

pTmp[i]+=('a'-'A');

}

}

Result=pTmp;

delete []pTmp;

return Result;

}

file://-------------------------------------------------------------------------

CString CString::StringOfChar(char ch, int count)const

{

CString str;

char *pTmp;

pTmp=new char[count+1];

memset(pTmp,0x00,count+1);

for(int i=0;i<count;i++)

{

pTmp[i]=ch;

}

str=pTmp;

delete []pTmp;

return str;

}

file://-----------------------------------------------------------------------

file://返回被分隔符分割的字符串,如

file://GetDelimitedString("ab,cd,ef",",",1) 返回"cd"

file://GetDelimitedString("ab,,ef",",",1) 返回"ef"

CString CString::GetDelimitedString(const char *delimiter,int index)

{

CString strResult;

int iIndex=0;

int iLength=Length()+1;

char *pSource=new char[iLength];

char *p=NULL;

memset(pSource,0,iLength);

memcpy(pSource,_data,iLength);

p=strtok(pSource,delimiter);

while(p)

{

if(iIndex==index)

{

strResult=CString(p);

break;

}

iIndex++;

p=strtok(NULL,delimiter);

}

delete []pSource;

pSource=NULL;

return strResult;

}

file://-----------------------------------------------------------------------

#ifdef TEST

main()

{

char *p="test";

CString strA("1234");

CString strB="123456";

strA=strB.GetDelimitedString("/",1);

CString strF;

strF.sprintf("[%s]",strA.c_str());

strF=strA.StringOfChar('A',3);

printf("[%s]\n",strF.c_str());

}

#endif

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有