mystring.cpp
来自「一个很好的string类,类似与Windows 下CString」· C++ 代码 · 共 354 行
CPP
354 行
#include <string.h>#include <stdarg.h>#include <stdio.h>#include "MyString.h"CString::CString():_data(0){ _data=new char[1]; _data[0]='\0';}CString::CString(const char *str):_data(0){ if(str) { _data=new char[strlen(str)+1]; strcpy(_data,str); } else { _data=new char[1]; _data[0]='\0'; }}CString::CString(CString &str):_data(0){ //必须在拷贝函数中为动态分配空间的指针分配空间 _data=new char[str.GetLength()+1]; strcpy(_data,str._data);}CString::~CString(){ delete []_data;}//return the length of this string,not counting the null-terminating character.int CString::GetLength() const{ return strlen(_data);}CString & CString::operator=(const CString &str){ if(this==&str) //can't be "this==str" { return *this; } delete []_data;//for the previous 'new' _data=new char[str.GetLength()+1]; strcpy(_data,str._data); return *this;}//the value returned by c_str points to the internal character//array referenced by the _data propertyconst char * CString::c_str()const{ return _data;}CString& CString::Format(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.GetLength()+str.GetLength()+1]; memset(_data,0x00,tmp.GetLength()+str.GetLength()+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.GetLength()+strlen(str)+1]; memset(_data,0x00,tmp.GetLength()+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.GetLength()+rhs.GetLength()+1]; memset(_data,0x00,tmp.GetLength()+rhs.GetLength()+1); strcpy(_data,tmp.c_str()); strcat(_data,rhs.c_str()); return *this;}char &CString::operator [](const int idx){ if( (idx<0) || (idx>GetLength()-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 <(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;}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[GetLength()+1]; memset(pTmp,0x00,GetLength()+1); strcpy(pTmp,_data); pDest=pTmp; for(int i=0;i<GetLength();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[GetLength()+1]; memset(pTmp,0x00,GetLength()+1); strcpy(pTmp,_data); pDest=pTmp+GetLength()-1; for(int i=GetLength();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[GetLength()+1]; memset(pTmp,0x00,GetLength()+1); strcpy(pTmp,_data); pDest=pTmp; for(int i=0;i<GetLength();i++) { if( (*pDest==' ') || (*pDest=='\n') || (*pDest=='\t') ) pDest++; else break; } p=pTmp+GetLength()-1; for(int j=GetLength();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[GetLength()+1]; memset(pTmp,0x00,GetLength()+1); strcpy(pTmp,_data); for(int i=0;i<GetLength();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[GetLength()+1]; memset(pTmp,0x00,GetLength()+1); strcpy(pTmp,_data); for(int i=0;i<GetLength();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<count;i++) { pTmp[i]=ch; } str=pTmp; delete []pTmp; return str;}//返回被分隔符分割的字符串,如//GetDelimitedString("ab,cd,ef",",",1) 返回"cd"//GetDelimitedString("ab,,ef",",",1) 返回"ef"CString CString::GetDelimitedString(const char *delimiter,int index){ CString strResult; int iIndex=0; int iLength=GetLength()+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;}//////////////////////////////////////////////////////////////////////////////int CString::Find(const char cCh,int nStart=0){ return -1;}int CString::Find(const char *pStr,int nStart=0){ return -1;}void CString::MakeReverse(){}CString CString::Left(int nCount){ CString strResult; return strResult;}CString CString::Right(int nCount){ CString strResult; return strResult;}CString CString::Mid(int nStart,int nCount){ CString strResult; return strResult;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?