C++下操作字符串的超级容易类~~~~~[2]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 lostinet 的 blog

   len=length-start;
  char* buf=new char[len+1];
  memcpy(buf,pstring+start,len);
  buf[len]=0;
  jstring* pstr=new jstring(*pallocator,len,buf);
  delete buf;
  return * pstr;
 }

 jstring& lower()
 {
  jstring* pstr=new jstring(*pallocator);
  strcpy(pstr->getbuffersetlength(length),pstring);
  strlwr(pstr->getbuffer());
  return * pstr;
 }

 jstring& upper()
 {
  jstring* pstr=new jstring(*pallocator);
  strcpy(pstr->getbuffersetlength(length),pstring);
  strupr(pstr->getbuffer());
  return * pstr;
 }
/****************************************************************/

 double number()
 {
  return strtod(pstring,0);
 }

 jstring& convert(bool b)
 {
  return * new jstring(*pallocator,b?4:5,b?"true":"false");
 }
 jstring& convert(double d)
 {
  char buf[64];
  sprintf(buf,"%g",d);
  return * new jstring(*pallocator,strlen(buf),buf);
 }
 jstring& convert(int n)
 {
  char buf[64];
  sprintf(buf,"%d",n);
  return * new jstring(*pallocator,strlen(buf),buf);
 }
 jstring& convert(const char* str)
 {
  return * new jstring(*pallocator,strlen(str),str);
 }

/****************************************************************/

 //所有的char*输入,地址都必须不为0

 jstring& operator + (jstring& str)
 {
  //if(str.pallocator!=pallocator)throw("jstring& operator + (jstring& str) 不能使用不同一条链的对象");
  //注意:两条链的元素相加,返回的元素属于第一条链
  jstring* pres=new jstring(*pallocator,length+str.length,pstring);
  strcat(pres->pstring,str.pstring);
  return *pres;
 }
 jstring& operator + (const char* str)
 {
  jstring* pres=new jstring(*pallocator,length+strlen(str),pstring);
  strcat(pres->pstring,str);
  return *pres;
 }
 jstring& operator + (bool b)
 {
  jstring* pstr=new jstring(*pallocator,length+(b?4:5),pstring);
  strcat(pstr->pstring,(b?"true":"false"));
  return *pstr;
 }
 jstring& operator + (double d)
 {
  char buf[64];
  sprintf(buf,"%g",d);
  return *this+buf;
 }
 jstring& operator + (int n)
 {
  char buf[64];
  sprintf(buf,"%d",n);
  return *this+buf;
 }
 jstring& operator += (bool b)
 {
  *this+=b?"true":"false";
  return *this;
 }
 jstring& operator += (double d)
 {
  char buf[64];
  sprintf(buf,"%g",d);
  *this+=buf;
  return *this;
 }
 jstring& operator += (int n)
 {
  char buf[64];
  sprintf(buf,"%d",n);
  *this+=buf;
  return *this;
 }
 jstring& operator += (const char *str)
 {
  length=length+strlen(str);
  char* pstr=new char[length+1];
  strcpy(pstr,pstring);
  strcat(pstr,str);
  delete pstring;
  pstring=pstr;
  return *this;
 }
 jstring& operator = (bool b)
 {
  if(b)
   *this="true";
  else
    *this="false";
  return *this;
 }
 jstring& operator = (double d)
 {
  char buf[64];
  sprintf(buf,"%g",d);
  *this=buf;
  return *this;
 }
 jstring& operator = (int n)
 {
  char buf[64];
  sprintf(buf,"%d",n);
  *this=buf;
  return *this;
 }
 jstring& operator = (jstring& str)
 {
  delete pstring;
  length=str.length;
  pstring=new char[length+1];
  strcpy(pstring,str.pstring);
  return *this;
 }
 jstring& operator = (const char* str)
 {
  delete pstring;
  length=strlen(str);
  pstring=new char[length+1];
  strcpy(pstring,str);
  return *this;
 }

/****************************************************************/

 bool comparenocase(const char* str)
 {
  return strcmpi(pstring,str)==0;

本文关键:string,C++,Lostinet,托管
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top