CString类

/* 

*程序作者:harite
*Email:Harite.K@gmail.com
*程序名称:CString.h
*程序功能:CString类定义头文件
*程序说明:使用Editplus编写,Dev C++4.9.9.2&MinGW3.4.2与windows xp sp2下编译通过,不足之处请来信指出.
*Thank All The People
*/

#include <cstdio>
#include <cstring>
#include <iostream>

class CString
{
public:

/*声明构造函数 Begin*/
 CString();
 CString(const char *pStr);
 CString(CString &CStringSrc);
 CString(const char ch, const unsigned int uiRepeat = 1);
 CString(const char *pStr, unsigned int uiLen, unsigned int uiBegin = 0);
 CString(CString &CStringSrc, unsigned int uiLen, unsigned int uiBegin = 0);
 /*声明构造函数 End*/
 
 /*声明析构函数 Begin*/
 ~CString();
 /*声明析构函数 End*/

 /*声明成员函数 Begin*/
 unsigned int GetLength(void);
 char *GetBuffer(void);
 bool IsEmpty(void);
 void Empty(void);
 char GetAt(unsigned int uiIndex);
 void SetAt(unsigned int uiIndex, char ch);
 //用于比较的函数
 int Compare(const char *str);
 int Compare(CString &Str);
 int CompareNoCase(const char *str);
 int CompareNoCase(CString &Str);
 //字符串截取函数
 CString &Right(unsigned int uiLength);
 CString &Left(unsigned int uiLength);
 CString &Mid(unsigned int uiBegin);
 CString &Mid(unsigned int uiBegin, unsigned int uiLength);
 //大小转换函数
 CString &MakeUpper(void);
 CString &MakeLower(void);
 CString &MakeReverse(void);
 //字符串修饰(包括置换,删除,添加等)
 CString &Replace(char chOld, char chNew);
 CString &Replace(char *pOld, char *pNew);
 CString &Insert(unsigned int uiIndex, char ch);
 CString &Insert(unsigned int uiIndex, char *str);
 CString &Remove(char ch);
 CString &Delete(unsigned int uiIndex, unsigned int uiCount = 1);
 CString &TrimLeft(void);
 CString &TrimLeft(char ch);
 CString &TrimLeft(char *str);
 CString &TrimRight(void);
 CString &TrimRight(char ch);
 CString &TrimRight(char *str);
 //查找函数
 int Find(char ch, unsigned int uiBegin = 0);
 int Find(char *str, unsigned int uiBegin = 0);
 int ReverseFind(char ch);
 int FindOneOf(char *str);
 /*声明成员函数 End*/
 
 /*声明重载的操作符 Begin*/
 //作为成员函数
 CString& operator= (const char ch);
 CString&   operator= (const char *str);
 CString&   operator= (CString &Str);
 CString&     operator+=(const char ch);
 CString&   operator+=(const char *str);
 CString&   operator+=(CString &Str);
 const char& operator[](unsigned int n);
 //作为友元
 friend CString&operator+ (CString &Str1, CString &Str2);
 friend CString&operator+ (CString &Str, const char *str);
 friend CString&operator+ (const char *str, CString &Str);
 friend CString&operator+ (CString &Str, char ch);
 friend CString&operator+ (char ch, CString &Str);
 friend bool operator==(CString &Str1, CString &Str2);
 friend bool operator==(CString &Str, const char *str);
 friend bool operator==(const char *str, CString &Str);
 friend bool operator!=(const CString &Str, const CString &Str);
 friend bool operator!=(const CString &Str, const char *str);
 friend bool operator!=(const char *str, const CString &Str);
 friend bool operator< (const CString &Str1, const CString &Str2);
 friend bool operator< (const CString &Str, const char *str);
 friend bool operator< (const char *str, const CString &Str);
 friend bool operator> (const CString &Str1, const CString &Str2);
 friend bool operator> (const CString &Str, const char *str);
 friend bool operator> (const char *str, const CString &Str);
 friend bool operator<=(const CString &Str1, const CString &Str2);
 friend bool operator<=(const CString &Str, const char *str);
 friend bool operator<=(const char *str, const CString &Str);
 friend bool operator>=(const CString &Str1, const CString &Str2);
 friend bool operator>=(const CString &Str, const char *str);
 friend bool operator>=(const char *str, const CString &Str);
/*声明重载的操作符 End*/

private:

 char *pString; //指向存储空间的首地址
 CString &Trim(int mode, char ch);
};

/*声明重载的IO函数 Begin*/
std::ostream& operator<<(std::ostream &o, CString &Str);
std::istream& operator>>(std::istream &i, CString &Str);
/*声明重载的IO函数 End*/

219.149.12.*

3楼

/* 

*程序作者:harite
*Email:Harite.K@gmail.com
*程序名称:CString.cpp
*程序功能:CString类实现文件
*程序说明:使用Editplus编写,Dev C++4.9.9.2&MinGW3.4.2与windows xp sp2下编译通过,不足之处请来信指出.
*Thank All The People
*/
#include "CString.h"

using namespace std;

/*定义构造函数 Begin*/

//无参构造函数,默认分配1个char的空间
CString::CString(){
pString = new char[1];
 pString[0] = '/0';
}

//以pStr所指向的字符串构造CString对象
CString::CString(const char *pStr){
pString = new char[strlen(pStr)+1];
strcpy(pString, pStr);
}

//以一个CString类构造新的CString对象
CString::CString(CString &CStringSrc){
pString = new char[CStringSrc.GetLength()+1];
strcpy(pString, CStringSrc.pString);
}

//以一个字符ch构造CString对象,uiRepeat为字符ch重复次数,省略这个参数使uiRepeat为1
CString::CString(const char ch, const unsigned int uiRepeat){
pString = new char[uiRepeat+1];
 int i;
 for(i=0; i<uiRepeat; i++)
 pString[i] = ch;

 pString[i] = '/0';
}

//以指针pStr所指向的字符串为基础,提取其中从uiBegin位置开始的uiLength个字符,用于构造CString对象
CString::CString(const char *pStr, //接受的字符串
 unsigned int uiLength, //接受的字符长度
 unsigned int uiBegin){ //接受的开始位置,默认为0,即从pStr的第一个空间开始复制
  unsigned int uiLen = strlen(pStr);

 if(uiBegin>uiLen)
 uiBegin = uiLen;
 if(uiLength>uiLen-uiBegin)
 uiLength = uiLen-uiBegin;

 pString = new char[uiLength+1];

 strncpy(pString, pStr+uiBegin, uiLength);
 pString[uiLength] = '/0';
}

//以一个CString对象为基础,提取其缓冲区中从uiBegin位置开始的uiLength个字符,用于构造新的CString对象
CString::CString(CString &CStringSrc, //接受的CString对象
 unsigned int uiLength, //接受的字符长度
 unsigned int uiBegin){ //接受的开始位置,默认为0,即从pStr的第一个空间开始复制
 unsigned int uiLen = CStringSrc.GetLength();

 if(uiBegin>uiLen)
 uiBegin = uiLen;
 if(uiLength>uiLen-uiBegin)
 uiLength = uiLen-uiBegin;

 pString = new char[uiLength+1];

 strncpy(pString, CStringSrc.pString+uiBegin, uiLength);
 pString[uiLength] = '/0';
}
/*定义构造函数 End*/

/*定义析构函数 Begin*/
//CString类的析构函数,删除所使用的缓冲区
CString::~CString(){
if(pString){
delete []pString;
}
}
/*定义析构函数 End*/

/*定义成员函数 Begin*/
//无参数,返回pString所指向的缓冲区的大小,不包括'/0'
unsigned int CString::GetLength(void){
 return strlen(pString);
}

//无参数,返回pString指针,主要用于重载<<操作符
char *CString::GetBuffer(void){
 return pString;
}

//无参数,判断pString所指向的缓冲区是否为空,为空则返回true,否则返回false
bool CString::IsEmpty(void){
return GetLength()?false:true;
}

//无参数,将pString所指向的字符串置空
void CString::Empty(void){
if(pString){
delete []pString;
}
pString = new char[1];
 pString[0] = '/0';
}

//接受一个索引值uiIndex,返回pString[uiIndex],若uiIndex越界,则返回pString[GetLength()]
char CString::GetAt(unsigned int uiIndex){
if(uiIndex>=GetLength()){
uiIndex = GetLength()-1;
}

return *(pString+uiIndex);
}

//接受一个索引值uiIndex,将pString[uiIndex]置为ch,若uiIndex越界,则将pString[GetLength()]置为ch

219.149.12.*

4楼

void CString::SetAt(unsigned int uiIndex, char ch){
if(uiIndex>=GetLength()){
uiIndex = GetLength()-1;
}

pString[uiIndex] = ch;
}

//用于字符串比较
//返回与指针str所指字符串比较之后的结果,相同返回0,小于返回-1,大于返回1
int CString::Compare(const char *str){
return strcmp(pString, str);
}

//返回与对象Str比较之后的结果,相同返回0,小于返回-1,大于返回1
int CString::Compare(CString &Str){
return strcmp(pString, Str.pString);
}

//返回与指针str所指字符串比较之后的结果(忽略大小写),相同返回0,小于返回-1,大于返回1
int CString::CompareNoCase(const char *str){
char *tmp1 = new char[GetLength()+1];
strcpy(tmp1, pString);
tmp1 = strlwr(tmp1);

char *tmp2 = new char[strlen(str)];
strcpy(tmp2, str);
tmp2 = strlwr(tmp2);

int n = strcmp(tmp1, tmp2);

delete tmp1;
delete tmp2;

return n;
}

//返回与对象Str比较之后的结果(忽略大小写),相同返回0,小于返回-1,大于返回1
int CString::CompareNoCase(CString &Str){
char *tmp1 = new char[GetLength()+1];
strcpy(tmp1, pString);
tmp1 = strlwr(tmp1);

char *tmp2 = new char[Str.GetLength()+1];
strcpy(tmp2, Str.pString);
tmp2 = strlwr(tmp2);

int n = strcmp(tmp1, tmp2);

delete tmp1;
delete tmp2;

return strcmp(tmp1, tmp2);
}

//从右测开始取当前对象字符串,长度为uiLength
CString &CString::Right(unsigned int uiLength)
{
if(uiLength>=GetLength()){
Mid(0, uiLength);
return *this;
}
 Mid(this->GetLength()-uiLength, uiLength);
 return *this;
}

//从左侧开始取当前对象字符串,长度为uiLength
CString &CString::Left(unsigned int uiLength)
{
 Mid(0, uiLength);
 return *this;
}

//从左侧索引uiBegin开始取当前对象字符串
CString &CString::Mid(unsigned int uiBegin){
Mid(uiBegin, GetLength());
 return *this;
}

//从左侧索引uiBegin开始取当前对象字符串,共取uiLength个
CString &CString::Mid(unsigned int uiBegin, unsigned int uiLength)
{
 int iLen = GetLength();

 if(uiBegin>iLen)
 uiBegin = iLen;
 if(uiLength>(iLen-uiBegin))
 uiLength = iLen-uiBegin;

 char *tmp = new char[uiLength+1];

 strncpy(tmp, pString+uiBegin, uiLength);
 tmp[uiLength] = '/0';

 delete pString;
 pString = tmp;
 return *this;
}

//将当前对象中的字符串全部置为大写
CString &CString::MakeUpper(void){
pString = strupr(pString);
return *this;
}

//将当前对象中的字符串全部置为小写
CString &CString::MakeLower(void){
pString = strlwr(pString);
return *this;
}

//将当前对象中的字符串倒置
CString &CString::MakeReverse(void){
pString = strrev(pString);
return *this;
}

//替换对象字符串中的字符chOld为chNew
CString &CString::Replace(char chOld, char chNew){
for(int i = 0;i<GetLength();i++){
 if(GetAt(i)==chOld){
 SetAt(i, chNew);
 }
 }
 return *this;
}

//替换对象字符串中的字符串pOld为pNew
CString &CString::Replace(char *pOld, char *pNew){
unsigned int uiTmp;
while(true){
 uiTmp = Find(pOld);
 if(uiTmp==-1){
 break;
 }
 Delete(uiTmp, strlen(pOld));
 Insert(uiTmp, pNew);
}
return *this;
}

//删除对象字符串中的字符ch
CString &CString::Remove(char ch){
bool flag;

219.149.12.*

5楼


do{
flag = false;
for(int i = 0;i<GetLength();i++){
if(GetAt(i)==ch){
 Delete(i);
 flag = true;
 }
 }
}while(flag);

return *this;
}

//删除对象字符串中从索引uiIndex开始共uiCount个字符
CString &CString::Delete(unsigned int uiIndex, unsigned int uiCount){
 unsigned int uiLen = GetLength();

 if(uiIndex>=uiLen){
 return *this;
 }
 if(uiCount==0){
 return *this;
 }
 
 if(uiCount>uiLen-uiIndex){
 uiCount = uiLen-uiIndex;
 }

 char *tmp = new char[uiLen-uiCount+1];

 strncpy(tmp, pString, uiIndex);
 tmp[uiIndex] = '/0';
 strcat(tmp, pString+uiIndex+uiCount);

 delete pString;
 pString = tmp;
 return *this;
}

//在对象字符串索引为uiIndex的地方插入字符串str
CString &CString::Insert(unsigned int uiIndex, char *str){
 unsigned int thisLen = GetLength();
 unsigned int uiLen = strlen(str);

 if(uiIndex > thisLen){
 uiIndex = thisLen;
 }

 char *tmp = new char[thisLen + uiLen + 1];

 strncpy(tmp, pString, uiIndex);
 tmp[uiIndex] = '/0';
 strcat(tmp, str);
 strcat(tmp, pString+uiIndex);

 delete pString;

 pString = tmp;
 return *this;
}

//在对象字符串索引为uiIndex的地方插入字符ch
CString &CString::Insert(unsigned int uiIndex, char ch){
unsigned int thisLen = GetLength();

 if(uiIndex > thisLen){
 uiIndex = thisLen;
 }

 char *tmp = new char[thisLen+1+1];

 strncpy(tmp, pString, uiIndex);
 tmp[uiIndex] = ch;
 tmp[uiIndex+1] = '/0';
 strcat(tmp, pString+uiIndex);

 delete pString;

 pString = tmp;
 return *this;
}

//在对象字符串中,从索引uiBegin开始,返回ch第一次出现的位置,省略uiBegin使其为默认的0,未找到返回-1
int CString::Find(char ch, unsigned int uiBegin){
unsigned int uiTmp;
char *tmp = strchr(pString+uiBegin, ch);
if(tmp==NULL){
return -1;
}
uiTmp = GetLength()-strlen(tmp);
delete tmp;
return uiTmp;
}

//在对象字符串中,从索引uiBegin开始,返回字符串str第一次出现的位置,省略uiBegin使其为默认的0,未找到返回-1
int CString::Find(char *str, unsigned int uiBegin){
unsigned int uiTmp;
char *tmp = strstr(pString+uiBegin, str);
if(tmp==NULL){
return -1;
}
uiTmp = GetLength()-strlen(tmp);
delete tmp;
return uiTmp;
}

//反向查找字符ch,并返回在其在对象字符串中的索引位置,未找到返回-1
int CString::ReverseFind(char ch){
CString tmp(*this);
tmp.MakeReverse();
if(Find(ch)==-1){
return -1;
}
return GetLength()-1-tmp.Find(ch);
}

//查找str所指向的字符串包含的字符,返回第一次出现的索引值,未找到返回-1
int CString::FindOneOf(char *str){
for(int i = 0;i<strlen(str);i++){
if(Find(str[i])==-1){
continue;
}else{
return Find(str[i]);
}
}

return -1;
}

//去除对象字符串左侧的字符ch
CString &CString::TrimLeft(char ch){
Trim(1, ch);
return *this;
}

//去除对象字符串左侧的换行,空格,制表字符
CString &CString::TrimLeft(void){
Trim(1, '/n');
Trim(1, ' ');
Trim(1, '/t');
return *this;
}

//去除对象字符串左侧的位于str所指向的字符串中的字符
CString &CString::TrimLeft(char *str){
for(int i = 0;i<strlen(str);i++){
Trim(1, str[i]);
}
return *this;
}


219.149.12.*

6楼

//去除对象字符串右侧的换行,空格,制表字符
CString &CString::TrimRight(void){
Trim(2, '/n');
Trim(2, ' ');
Trim(2, '/t');
return *this;
}

去除对象字符串右侧的字符ch
CString &CString::TrimRight(char ch){
Trim(2, ch);
return *this;
}

//去除对象字符串右侧的位于str所指向的字符串中的字符
CString &CString::TrimRight(char *str){
for(int i = 0;i<strlen(str);i++){
Trim(2, str[i]);
}
return *this;
}

//1代表LEFT,2代表RIGHT
CString &CString::Trim(int mode, char ch){
 unsigned int uiBegin = 0;
 unsigned int uiEnd = GetLength()-1;

 if(mode==1){
 while(pString[uiBegin]==ch && uiBegin<=uiEnd)
 uiBegin++;
 }else if(mode==2){
 while(pString[uiEnd]==ch && uiEnd>=uiBegin)
 uiEnd--;
 }else{
 return *this;
 }

 unsigned int uiLen = uiEnd-uiBegin+1;
 char *tmp = new char[uiLen+1];
 strncpy(tmp, pString+uiBegin, uiLen);
 tmp[uiLen] = '/0';

 delete pString;
 pString = tmp;
 return *this;
}
/*定义成员函数 End*/

/*定义重载的运算符 Begin*/
CString& CString::operator=(const char ch){
 if(pString){
 delete pString;
 }

 pString = new char[2];
 pString[0] = ch;
 pString[1] = '/0';

 return *this;
}

CString& CString::operator=(const char *str){
 if(pString){
 delete pString;
 }
 
 pString = new char[strlen(str)+1];
 strcpy(pString, str);
 
 return *this;
}

CString& CString::operator=(CString &Str){
 if(pString){
 delete pString;
 }
 
 pString = new char[Str.GetLength()+1];
 strcpy(pString, Str.pString);
 
 return *this;
}

CString& operator+(CString &Str, const char *str){
 CString *tmp = new CString(Str);
 *tmp += str;
 return *tmp;
}

CString& operator+(const char *str, CString& Str){
 CString *tmp = new CString(str);
 *tmp+=Str;
 return *tmp;
}

CString& operator+(CString &Str1, CString &Str2){
 CString *tmp = new CString(Str1);
 *tmp += Str2;
 return *tmp;
}

CString& operator+(CString &Str, char ch){
CString *tmp = new CString(Str);
*tmp+=ch;
return *tmp;
}

CString& operator+(char ch, CString &Str){
CString *tmp = new CString;
*tmp+=ch;
*tmp+=Str;
return *tmp;
}

CString& CString::operator+=(const char ch){
 char *tmp = pString;
 int thisLen = this->GetLength();

 pString = new char[thisLen + 2];

 strcpy(pString, tmp);
 pString[thisLen] = ch;
 pString[thisLen+1] = '/0';

 if(tmp){
 delete tmp;
 }

 return *this;
}

CString& CString::operator+=(const char *str){
 char *tmp = pString;

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

 strcpy(pString, tmp);
 strcat(pString, str);

 if(tmp){
 delete tmp;
 }
 
 return *this;
}

CString& CString::operator+=(CString &Str){
 char *tmp = pString;
 pString = new char[strlen(tmp) + Str.GetLength() + 1];

 strcpy(pString, tmp);
 strcat(pString, Str.pString);

 if(tmp){
 delete tmp;
 }
 
 return *this;
}

const char &CString::operator[](unsigned int n){
 if(n>=GetLength())
 n = GetLength()-1;
 return *(pString + n);
}

bool operator==(CString &Str, const char *str){
 return strcmp(Str.pString, str)==0;
}

bool operator==(const char *str, CString &Str){
 return strcmp(str, Str.pString)==0;
}

bool operator==(CString &Str1, CString &Str2){
 return strcmp(Str1.pString, Str2.pString)==0;
}

bool operator!=(const CString &Str, const char *str){
 return strcmp(Str.pString, str)!=0;
}

bool operator!=(const char *str, const CString &Str){
 return strcmp(str, Str.pString)!=0;
}

bool operator!=(const CString &Str1, const CString &Str2){
 return strcmp(Str1.pString, Str2.pString)!=0;
}

bool operator<(const CString &Str, const char *str){
 return strcmp(Str.pString, str)<0;
}

bool operator<(const char *str, const CString &Str){
 return strcmp(str, Str.pString)<0;
}

bool operator<(const CString &Str1, const CString &Str2){
 return strcmp(Str1.pString, Str2.pString)<0;
}

bool operator>(const CString &Str, const char *str){
 return strcmp(Str.pString, str)>0;
}

bool operator>(const char *str, const CString &Str){
 return strcmp(str, Str.pString)>0;
}

bool operator>(const CString &Str1, const CString &Str2) {
 return strcmp(Str1.pString, Str2.pString)>0;
}

bool operator<=(const CString &Str, const char *str) {
 return strcmp(Str.pString, str)<=0;
}

bool operator<=(const char *str, const CString &Str){
 return strcmp(str, Str.pString)<=0;
}

bool operator<=(const CString &Str1, const CString &Str2){
 return strcmp(Str1.pString, Str2.pString)<=0;
}

bool operator>=(const CString &Str, const char *str){
 return strcmp(Str.pString, str)>=0;
}

bool operator>=(const char *str, const CString &Str){
 return strcmp(str, Str.pString)>=0;
}

bool operator>=(const CString &Str1, const CString &Str2){
 return strcmp(Str1.pString, Str2.pString)>=0;
}

//IO流
ostream& operator<<(ostream &o, CString &Str){
 return o<<Str.GetBuffer();
}

istream& operator>>(istream &i, CString &Str){
 char tmp[256];
 i>>tmp;
 Str = tmp;
 return i;
}

219.149.12.*

7楼

/* 

*程序作者:harite
*Email:Harite.K@gmail.com
*程序名称:main.cpp
*程序功能:演示自己实现的这个CString类的使用方法
*程序说明:使用Editplus编写,Dev C++4.9.9.2&MinGW3.4.2与windows xp sp2下编译通过,不足之处请来信指出.
*Thank All The People
*/
#include <cstdlib>
#include <iostream>
#include "CString.h"

using namespace std;

int main(int argc, char *argv[])
{
 CString s1;
 CString s2("I am harite.");
 CString s3(s2);
 CString s4('x', 12);
 CString s5("houhou~~ I like Music.", 13, 9);
 CString s6(s2, 6, 5);
 cout<<"使用CString()构造函数"<<endl<<s1<<endl<<"长度为:"<<s1.GetLength()<<endl;
 system("PAUSE");
 cout<<"使用字符串I am harite.构造对象"<<endl<<s2<<endl<<"长度为:"<<s2.GetLength()<<endl;
 system("PAUSE");
 cout<<"使用一个已经存在的对象s2构造新的对象"<<endl<<s3<<endl<<"长度为:"<<s3.GetLength()<<endl;
 system("PAUSE");
 cout<<"使用字符x重复12次构造对象"<<endl<<s4<<endl<<"长度为:"<<s4.GetLength()<<endl;
 system("PAUSE");
 cout<<"提取字符串houhou~~ I like Music.从第9个索引位置开始共13个字符来构造对象"<<endl<<s5<<endl<<"长度为:"<<s5.GetLength()<<endl;
 system("PAUSE");
 cout<<"提取已经存在的对象s2/""<<s2<<"/",从第5个索引位置开始共6个字符来构造新对象"<<endl<<s6<<endl<<"长度为:"<<s6.GetLength()<<endl;
 system("PAUSE");
 s1 = "!!!It is my life...";
 cout<<"将字符串!!!It is my life...付给对象s1"<<endl<<s1.GetBuffer()<<endl<<"长度为:"<<s1.GetLength()<<endl;
 system("PAUSE");
 s1+="It is a new day";
 cout<<"加上字符串/"It is a new day/""<<endl<<s1<<endl<<"长度为:"<<s1.GetLength()<<endl;
 system("PAUSE");
 s1+='e';
 cout<<"再加上一个字符e"<<endl<<s1<<endl<<"长度为:"<<s1.GetLength()<<endl;
 system("PAUSE");
 s1.Delete(19, 16);
 cout<<"从第19个索引开始删除16个字符"<<endl<<s1<<endl<<"长度为:"<<s1.GetLength()<<endl;
 system("PAUSE");
 cout<<"取上面这个对象字符串的第9个字符"<<endl<<s1.GetAt(9)<<endl<<"长度为:"<<s1.GetLength()<<endl;
 system("PAUSE");
 s1.SetAt(9, '^');
 cout<<"将这个字符变为^"<<endl<<s1<<endl<<"长度为:"<<s1.GetLength()<<endl;
 system("PAUSE");
 s1.TrimRight('.');
 cout<<"去掉其右侧的."<<endl<<s1<<endl<<"长度为:"<<s1.GetLength()<<endl;
 system("PAUSE");
 s1.Remove('^');
 cout<<"删除^字符"<<endl<<s1<<endl<<"长度为:"<<s1.GetLength()<<endl;
 system("PAUSE");
 s1.Insert(9, 'm');
 cout<<"插入一个m字符"<<endl<<s1<<endl<<"长度为:"<<s1.GetLength()<<endl;
 system("PAUSE");
 s1.Right(13);
 cout<<"从右侧截取此对象13个字符"<<endl<<s1<<endl<<"长度为:"<<s1.GetLength()<<endl;
 system("PAUSE");
 s1.MakeUpper();
 cout<<"变为大写字符"<<endl<<s1<<endl<<"长度为:"<<s1.GetLength()<<endl;
 system("PAUSE");
 s1.MakeLower();
 cout<<"变为小写字符"<<endl<<s1<<endl<<"长度为:"<<s1.GetLength()<<endl;
 system("PAUSE");
 s1.Replace('i', 'I');
 cout<<"将i变为I"<<endl<<s1<<endl<<"长度为:"<<s1.GetLength()<<endl;
 system("PAUSE");
 s1.Replace("Is", "is");
 s1.Replace("lIfe", "life");
 cout<<"将Is变为is,lIfe变为life"<<endl<<s1<<endl<<"长度为:"<<s1.GetLength()<<endl;
 system("PAUSE");
 CString tmp1(s1);
 CString tmp2(s1);
 CString tmp3(s1);
 tmp1.Left(2);
 tmp2.Mid(2, 4);
 tmp3.Right(7);
 cout<<"将上面那个对象所含有字符串分为3段"<<endl<<tmp1<<endl<<"长度为:"<<tmp1.GetLength()<<endl<<tmp2<<endl<<"长度为:"<<tmp2.GetLength()<<endl<<tmp3<<endl<<"长度为:"<<tmp3.GetLength()<<endl;
 system("PAUSE");
 CString tmp;
 tmp = tmp1+tmp2+tmp3;
 cout<<"将3个对象用+连接后在=给一个新对象"<<endl<<tmp<<endl<<"长度为:"<<tmp.GetLength()<<endl;
 system("PAUSE");
 tmp.Empty();
 cout<<"将新对象置空"<<endl<<tmp<<endl<<"长度为:"<<tmp.GetLength()<<endl;
 system("PAUSE");
 cout<<"输入一个字符串(别带空格^):";
 cin>>tmp;
 cout<<"你输入的字符串为"<<endl<<tmp<<endl<<"长度为:"<<tmp.GetLength()<<endl;
 tmp.MakeReverse();
 cout<<"将其反置后为"<<endl<<tmp<<endl<<"长度为:"<<tmp.GetLength()<<endl;
 system("PAUSE");
 tmp.MakeReverse();
 cout<<"正常显示为"<<endl<<tmp<<endl<<"长度为:"<<tmp.GetLength()<<endl;
 system("PAUSE");
 int n = tmp.Find("harite");
 if(n!=-1){
 cout<<"其中含有字符串/"harite/""<<endl;
 }else{
 cout<<"其中含有字符串/"harite/""<<endl;
 }
 system("PAUSE");
 cout<<"使用[]操作符循环显示字符串内容"<<endl;
 for(int i = 0;i<tmp.GetLength();i++){
 cout<<tmp[i]<<endl;
 }
 system("PAUSE");
 return EXIT_SUCCESS;
}
///

 

 

 

 

 

CSTRING类

[日期:2009-05-14]来源:中嵌信息  作者:chinaeda_news[字体: ]

CSTRING.H
// Dream House World Static Model
#ifndef __CSTRING__
#define __CSTRING__

#include <iostream.h>

class StringIndexOutOfBounds { };

class CSTRING
{
public:
CSTRING( const char *cstring = "" ); // Constructor
CSTRING( const CSTRING & str ); // Copy constructor
~CSTRING( ) // Destructor
{ delete [ ] buffer; }

const CSTRING & operator+= ( const int number ); // apend number
const CSTRING & operator= ( const CSTRING & rhs ); // Copy
const CSTRING & operator+=( const CSTRING & rhs ); // Append

const char *c_str( ) const // Return C-style CSTRING
{ return buffer; }
int length( ) const // Return CSTRING length
{ return strLength; }

char operator[]( int k ) const; // Accessor operator[]
char & operator[]( int k ); // Mutator operator[]

enum { MAX_LENGTH = 1024 }; // Maximum length for input CSTRING

private:
char *buffer; // storage for characters
int strLength; // length of CSTRING (# of characters)
int bufferLength; // capacity of buffer
};

ostream & operator<<( ostream & out, const CSTRING & str ); // Output
istream & operator>>( istream & in, CSTRING & str ); // Input
istream & getline( istream & in, CSTRING & str ); // Read line

bool operator==( const CSTRING & lhs, const CSTRING & rhs ); // Compare ==
bool operator!=( const CSTRING & lhs, const CSTRING & rhs ); // Compare !=
bool operator< ( const CSTRING & lhs, const CSTRING & rhs ); // Compare <
bool operator<=( const CSTRING & lhs, const CSTRING & rhs ); // Compare <=
bool operator> ( const CSTRING & lhs, const CSTRING & rhs ); // Compare >
bool operator>=( const CSTRING & lhs, const CSTRING & rhs ); // Compare >=

#endif // __CSTRING__

//CSTRING.CPP
// Dream House World Static Model
#include <string.h>
#include <stdio.h>
#include "CSTRING.h"


CSTRING::CSTRING( const char * cstring )
{
if( cstring == NULL )
cstring = "";
strLength = strlen( cstring );
bufferLength = strLength + 1;
buffer = new char[ bufferLength ];
strcpy( buffer, cstring );
}

CSTRING::CSTRING( const CSTRING & str )
{
strLength = str.length( );
bufferLength = strLength + 1;
buffer = new char[ bufferLength ];
strcpy( buffer,str.buffer );
}

const CSTRING & CSTRING::operator=( const CSTRING & rhs )
{
if( this != &rhs )
{
if( bufferLength < rhs.length( ) + 1 )
{
delete [ ] buffer;
bufferLength = rhs.length( ) + 1;
buffer = new char[ bufferLength ];
}
strLength = rhs.length( );
strcpy( buffer, rhs.buffer );
}
return *this;
}

const CSTRING & CSTRING::operator+=( const CSTRING & rhs )
{
if( this == &rhs )
{
CSTRING copy( rhs );
return *this += copy;
}

int newLength = length( ) + rhs.length( );
if( newLength >= bufferLength )
{
bufferLength = 2 * ( newLength + 1 );

char *oldBuffer = buffer;
buffer = new char[ bufferLength ];
strcpy( buffer, oldBuffer );
delete [ ] oldBuffer;
}

strcpy( buffer + length( ), rhs.buffer );
strLength = newLength;
return *this;
}

const CSTRING & CSTRING::operator+=(const int number)
{
char temp[20];
sprintf(temp,"%d",number);
CSTRING num( temp );
*this += num;
return *this;
}

char & CSTRING::operator[ ]( int k )
{
if( k < 0 || k >= strLength )
throw StringIndexOutOfBounds( );
return buffer[ k ];
}

char CSTRING::operator[ ]( int k ) const
{
if( k < 0 || k >= strLength )
throw StringIndexOutOfBounds( );
return buffer[ k ];
}

ostream & operator<<( ostream & out, const CSTRING & str )
{
return out << str.c_str();
}

istream & operator>>( istream & in, CSTRING & str )
{
char buf[ CSTRING::MAX_LENGTH + 1 ];
in >> buf;
if( !in.fail( ) )
str = buf;
return in;
}

istream & getline( istream & in, CSTRING & str )
{
char buf[ CSTRING::MAX_LENGTH + 1 ];
in.getline( buf, CSTRING::MAX_LENGTH );
if( !in.fail( ) )
str = buf;
return in;
}

bool operator==( const CSTRING & lhs, const CSTRING & rhs )
{
return strcmp( lhs.c_str( ), rhs.c_str( ) ) == 0;
}

bool operator!=( const CSTRING & lhs, const CSTRING & rhs )
{
return strcmp( lhs.c_str( ), rhs.c_str( ) ) != 0;
}

bool operator<( const CSTRING & lhs, const CSTRING & rhs )
{
return strcmp( lhs.c_str( ), rhs.c_str( ) ) < 0;
}

bool operator<=( const CSTRING & lhs, const CSTRING & rhs )
{
return strcmp( lhs.c_str( ), rhs.c_str( ) ) <= 0;
}

bool operator>( const CSTRING & lhs, const CSTRING & rhs )
{
return strcmp( lhs.c_str( ), rhs.c_str( ) ) > 0;
}

bool operator>=( const CSTRING & lhs, const CSTRING & rhs )
{
return strcmp( lhs.c_str( ), rhs.c_str( ) ) >= 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值