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

类别:VC语言 点击:0 评论:0 推荐:

/****************************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

 

本文地址:http://com.8s8s.com/it/it2431.htm