適合於Unix與Win32下的字串處理類,可以以此為基類進行擴充套件 (轉)
/****************************CString.h******************************/
#ifndef MyStringH
#define MyStringH
#ifndef
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); 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 bool operator <=(const CString& rhs) const;
bool operator ==(const CString& rhs) const;
bool operator >(const CString& rhs) const;
bool operator >=(const CString& rhs) const;
回被分隔符分割的字串,如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******************************/
#include "CString.h"
#include
#include
#include
CString::CString():_data(0)
{
_data=new char[1];
_data[0]='';
}
CString::CString(const char *str):_data(0)
{
if(str)
{
_data=new char[strlen(str)+1];
(_data,0x00,strlen(str)+1);
strcpy(_data,str);
}
else
{
_data=new char[1];
_data[0]='';
}
}
CString::CString(CString &str):_data(0)
{
須在複製中為動態分配空間的指標分配空間
_data=new char[str.Length()+1];
strcpy(_data,str._data);
}
CString::~CString()
{
delete []_data;
}
the length of this string,not counting the null-tenating character.
int CString::Length() const
{
return strlen(_data);
}
CString & CString::operator=(const CString &str)
{
if(this==&str) be "this==str"
{
return *this;
}
delete []_data;//for the previous 'new'
_data=new char[str.Length()+1];
strcpy(_data,str._data);
return *this;
}
value returned by c_str points to the internal character
referenced by the _data property
const char * CString::c_str()const
{
return _data;
}
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
}
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;
}
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;
}
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;
}
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];
}
bool CString::operator !=(const CString& rhs) const
{
if( strcmp(_data,rhs.c_str())!=0)
return true;
else
return false;
}
bool CString::operator {
if( strcmp(_data,rhs.c_str())<0)
return true;
else
return false;
}
bool CString::operator <=(const CString& rhs) const
{
if( strcmp(_data,rhs.c_str())<=0)
return true;
else
return false;
}
bool CString::operator ==(const CString& rhs) const
{
if( strcmp(_data,rhs.c_str())==0)
return true;
else
return false;
}
bool CString::operator >(const CString& rhs) const
{
if( strcmp(_data,rhs.c_str())>0)
return true;
else
return false;
}
bool CString::operator >=(const CString& rhs) const
{
if( strcmp(_data,rhs.c_str())>=0)
return true;
else
return false;
}
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
if( (*pDest==' ') || (*pDest=='n') || (*pDest=='t') )
pDest++;
else
break;
}
Result=pDest;
delete []pTmp;
return Result;
}
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;
}
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
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;
}
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
if( (pTmp[i]>='a') && (pTmp[i]<='z') )
{
pTmp[i]-=('a'-'A');
}
}
Result=pTmp;
delete []pTmp;
return Result;
}
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
if( (pTmp[i]>='A') && (pTmp[i]<='Z') )
{
pTmp[i]+=('a'-'A');
}
}
Result=pTmp;
delete []pTmp;
return Result;
}
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
pTmp[i]=ch;
}
str=pTmp;
delete []pTmp;
return str;
}
回被分隔符分割的字串,如
("ab,cd,ef",",",1) 返回"cd"
("ab,,ef",",",1) 返回"ef"
CString CString::GetDelimitedString(const char *delimiter,int index)
{
CString strResult;
int iIndex=0;
int iLength=Length()+1;
char *p=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;
}
#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
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-992885/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 分類擴充套件套件
- Spring(11) - Introductions進行類擴充套件方法Spring套件
- Swift快速為類擴充套件屬性Swift套件
- C# 擴充套件方法 借籤於 Objective-C 擴充套件類.C#套件Object
- HttpContext擴充套件類HTTPContext套件
- weex ios擴充套件類的作用iOS套件
- 可擴充套件的TextView,ExpandableTextView與Scroller類的使用套件TextView
- WCF擴充套件:行為擴充套件Behavior Extension套件
- 基於GenericAPIView以及五個檢視擴充套件類寫介面APIView套件
- 轉向Kotlin——列舉類和擴充套件Kotlin套件
- 轉載hp--unix下檔案系統的擴充套件套件
- dart系列之:dart類的擴充套件Dart套件
- Laravel 執行時類的功能擴充套件的實現Laravel套件
- javascript字串處理類JavaScript字串
- Java-IoUtil擴充套件工具類Java套件
- Scala由類的動態擴充套件想到型別類套件型別
- C#中的擴充套件類的理解C#套件
- dart系列之:你的地盤你做主,使用Extension對類進行擴充套件Dart套件
- iOS開發的分類和擴充套件iOS套件
- iOS分類(category)、類擴充套件(extension)、繼承的區別iOSGo套件繼承
- 基於AbstractProcessor擴充套件MapStruct自動生成實體對映工具類套件Struct
- PowerToys外掛擴充套件(類似Alfred)套件Alfred
- 五個檢視擴充套件類 LL套件
- 適用於開發者的最佳 Chrome 擴充套件工具Chrome套件
- 基於MongoDB.Driver的擴充套件MongoDB套件
- 基於 Bootstrap 的 UI 擴充套件:StyleBootstrapbootUI套件
- ASP中一個字串處理類(VBScript) (轉)字串
- 04.字串的擴充套件字串套件
- 進化計算中基於分類的預處理代理模型模型
- PHPWAMP安裝Redis擴充套件的方式與相關擴充套件的下載PHPRedis套件
- 基於 GatewayWorker 開發的 Laravel 擴充套件GatewayLaravel套件
- 基於PHP擴充套件的WAF實現PHP套件
- es6 字串的擴充套件字串套件
- 10.還不會擴充套件Dart中的類?套件Dart
- Golang中使用lua進行擴充套件Golang套件
- SpringBoot各類擴充套件點詳解Spring Boot套件
- Objective-C 類別(category)和擴充套件(Extension)ObjectGo套件
- C#新特性:匿名類和擴充套件方法C#套件