////////////////////////////////////////////////////////////////////// // Construction/Destruction
////////////////////////////////////////////////////////////////////// CLString::CLString() { }
CLString::CLString(const _CLString &str) {
erase();
_CLString::append(str); }
CLString::CLString(const TCHAR *pstr) {
if (pstr!=NULL) {
erase();
_CLString::append(pstr); } }
CLString::CLString(CString str)
{
if (!str.IsEmpty()) {
TCHAR *strbuf=str.GetBuffer(str.GetLength()); erase();
_CLString::append(strbuf); } }
CLString::CLString(TCHAR ch) {
erase();
_CLString::append(1,ch); }
CLString::CLString(int num) {
TCHAR databuf[256]={0}; TCHAR *pdata=databuf;
#ifdef UNICODE _itow(num,pdata,10); #else
_itoa(num,pdata,10); #endif
erase();
append(pdata); }
CLString::~CLString() { }
bool CLString::PeekContent(CLString &strcont,CLString startlabel,CLString endlabel, CLString::size_type startpos) {
if (empty()) {
return false; }
strcont.erase();
size_type spos=FindStr(startlabel,startpos); if (spos==CLString::npos) {
return false; }
size_type epos=EndLabelPos(startlabel,endlabel,spos+startlabel.length()); if (epos==CLString::npos) {
return false; }
strcont=substr(spos+startlabel.length(),epos-(spos+startlabel.length())); erase(spos,epos+endlabel.length()-spos);
return true; }
bool CLString::PeekContent(CLString &strcont,size_type &dispos, CLString startlabel,CLString endlabel, CLString::size_type startpos) {
if (empty()) {
return false; }
strcont.erase();
dispos=CLString::npos;
size_type spos=FindStr(startlabel,startpos); if (spos==CLString::npos) {
return false; }
dispos=spos;
size_type epos=EndLabelPos(startlabel,endlabel,spos+startlabel.length()); if (epos==CLString::npos) {
return false; }
strcont=substr(spos+startlabel.length(),epos-(spos+startlabel.length())); erase(spos,epos+endlabel.length()-spos);
return true; }
bool CLString::PeekContent_Unblocked(CLString &strcont,CLString startlabel,CLString endlabel, CLString::size_type startpos) {
if (empty()) {
return false; }
strcont.erase();
if (!PeekContent(strcont,startlabel,endlabel)) {
return false; }
size_type posendl=strcont.FindStr(_T(\">\")); if (posendl==npos) {
return false; }
strcont.erase(0,posendl+1);
return true; }
bool CLString::PeekContent(CLString &strcont,CLString &value, CLString startlabel,CLString endlabel) {
if (empty()) {
return false; }
strcont.erase(); value.erase();
if (!PeekContent(strcont,startlabel,endlabel)) {
return false; }
size_type posendl=strcont.FindStr(_T(\">\")); if (posendl==npos) {
return false; }
CLString strleft=strcont.substr(0,posendl+1); strcont.erase(0,posendl+1);
strleft.PeekContent(value,_T(\"\\\"\"),_T(\"\\\"\"));
return true; }
bool CLString::PeekContent(CLString &strcont,CLString &value, CLString valuelabel,CLString startlabel, CLString endlabel) {
if (empty()) {
return false; }
strcont.erase(); value.erase();
if (!PeekContent(strcont,startlabel,endlabel)) {
return false; }
size_type posendl=strcont.FindStr(_T(\">\")); if (posendl==npos) {
return false; }
CLString strleft=strcont.substr(0,posendl+1); strcont.erase(0,posendl+1);
size_type posvl=strleft.FindStr(valuelabel); if (posvl==npos) {
return false; }
strleft=strleft.substr(posvl,strleft.length()-posvl);
if(!strleft.PeekContent(value,_T(\"\\\"\"),_T(\"\\\"\"))) {
return false; }
return true; }
bool CLString::StartWith(CLString label) {
CLString strcopy=*this; strcopy.TrimLeft();
size_type pos=strcopy.FindStr(label); if (pos==CLString::npos ||pos!=0) {
return false; } return true; }
bool CLString::StartWithNum() {
CLString strcopy=*this;
strcopy.TrimLeft();
if (strcopy.length()<=0) {
return false; }
TCHAR firstch=strcopy.at(0);
if (firstch>=_T('0')&&firstch<=_T('9')) {
return true; }
return false; }
bool CLString::StartWithNum(size_type &nonumpos) {
CLString strcopy=*this; strcopy.TrimLeft(); CLString strnum; nonumpos=npos;
if (strcopy.length()<=0) {
return false; }
int i=0; TCHAR ch;
while (ich=strcopy.at(i);if (ch>=_T('0')&&ch<=_T('9')) { i++;
strnum+=ch; continue; }
break; }
if (i<=0) {
return false; }
nonumpos=FindStr(strnum)+strnum.length(); return true; }
bool CLString::EndWith(CLString label) {
CLString strcopy=*this; strcopy.TrimRight(); if (strcopy.length()<=0) {
return false; }
int len=label.length();
size_type pos=strcopy.length()-len; CLString strtemp=substr(pos,len); if (strtemp.compare(label)==0) {
return true; }
return false; }
void CLString::ReplaceStr(CLString labelold,CLString labelnew) {
size_type pos=FindStr(labelold); while (pos!=CLString::npos) {
replace(pos,labelold.length(),labelnew); pos=FindStr(labelold,pos); } }
void CLString::ReplaceStr_Unblocked(CLString labelold,CLString labelnew) {
size_type pos=FindStr(labelold); size_type posend=CLString::npos; while (pos!=CLString::npos) {
posend=FindStr(_T(\">\"),pos); if (posend!=CLString::npos) {
replace(pos,posend+1-pos,labelnew); }
pos=FindStr(labelold,pos); } }
void CLString::MakeUpStr() {
TCHAR uptch=_T('A'); TCHAR startch=_T('a'); TCHAR endtch=_T('z'); TCHAR tchtemp;
size_type i=0; int len=length(); for (i=0;itchtemp=at(i);if (tchtemp>=startch&&tchtemp<=endtch) {
tchtemp=(tchtemp-startch)+uptch; }
at(i)=tchtemp; } }
void CLString::MakeUpFirstChar() {
TrimLeft();
if (length()<=0) {
return; }
TCHAR uptch=_T('A'); TCHAR startch=_T('a'); TCHAR endtch=_T('z'); TCHAR tchtemp=at(0);
if (tchtemp>=startch&&tchtemp<=endtch) {
tchtemp=(tchtemp-startch)+uptch; }
at(0)=tchtemp; }
void CLString::MakeLowStr() {
TCHAR lowtch=_T('a'); TCHAR startch=_T('A'); TCHAR endtch=_T('Z'); TCHAR tchtemp;
size_type i=0; int len=length(); for (i=0;itchtemp=at(i);if (tchtemp>=startch&&tchtemp<=endtch) {
tchtemp=(tchtemp-startch)+lowtch; }
at(i)=tchtemp; } }
void CLString::MakeLowFirstChar() {
TrimLeft(); if (length()<=0) {
return;
}
TCHAR lowtch=_T('a'); TCHAR startch=_T('A');
TCHAR endtch=_T('Z'); TCHAR tchtemp=at(0);
if (tchtemp>=startch&&tchtemp<=endtch) {
tchtemp=(tchtemp-startch)+lowtch; }
at(0)=tchtemp; }
void CLString::TrimLeft() {
if (empty()) {
return; }
int i=0;
TCHAR startch; while (true) {
if (i>=length()) {
break; }
startch=at(i); if (startch!=_T(' ') &&startch!=_T('\') &&startch!=_T('\\r') &&startch!=_T('\\n')) {
break; } i++; }
if (i>0) {
erase(0,i); } }
void CLString::TrimRight() {
if (empty()) {
return;
}
int endpos=length()-1; int i=endpos; TCHAR startch; while (i>=0) {
startch=at(i);
if (startch!=_T(' ') &&startch!=_T('\') &&startch!=_T('\\r') &&startch!=_T('\\n')) {
break; } i--; }
erase(i+1,length()-i); }
_CLString::size_type CLString::FindStr(const CLString &str,size_type startpos) const {
size_type pos=CLString::npos; if (str.empty()) {
return pos; }
if (startpos<0||startpos>length()) {
return pos; }
TCHAR startch=str.at(0);
unsigned long len=str.length(); CLString strtemp;
TCHAR firstch;
for (int i=startpos;ifirstch=at(i);if (firstch==startch) {
strtemp=this->substr(i,len);
if (strtemp.compare(str)==0) {
pos=i; break; } } }
if (pos>=length()) {
pos=npos; }
return pos; }
_CLString::size_type CLString::ReverseFindStr(const CLString &str,size_type startpos)const {
if (str.empty()) {
return npos; }
if (startpos==0) {
startpos=this->length()-1; }
if (startpos<0||startpos>this->length()) {
return npos; }
int substrlen=str.length(); if (startposreturn npos; }CLString strtemp; while (true) {
strtemp=this->substr(startpos,substrlen); if (strtemp.compare(str)==0) {
break; }
startpos--;
if (startpos==CLString::npos) {
break; } }
return startpos; }
CString CLString::ToCString() {
CString result;
result.GetBuffer(length());
memcpy(result.GetBuffer(length()),c_str(),length()*sizeof(TCHAR)); result.ReleaseBuffer(); return result; }
CLString::size_type CLString::EndLabelPos(const CLString &startlabel, const CLString &endlabel, CLString::size_type startpos) {
CLString::size_type pos1=FindStr(endlabel,startpos); if (pos1==npos) {
return pos1; }
if (startlabel.compare(endlabel)==0) {
startpos=pos1+endlabel.length(); }
CLString::size_type pos2=FindStr(startlabel,startpos); if (pos2==npos) {
return pos1; }
if (pos1return pos1; }CLString strcopy=c_str();
int lastpos2=strcopy.ReverseFindStr(startlabel,pos1); int len=pos1+endlabel.length()-lastpos2;
strcopy.erase(lastpos2,len);
return len+strcopy.EndLabelPos(startlabel,endlabel,startpos); }
int CLString::DisCount(const CLString &substr,CLString::size_type startpos) {
int count=0;
if (startpos<0) {
startpos=0; }
while (true) {
startpos=FindStr(substr,startpos); if (startpos==npos) {
break; }
startpos+=substr.length(); count++; }
return count; }
int CLString::DisLabelCount(const CLString &startlabel,const CLString &endlabel, CLString::size_type startpos) {
int count=0;
if (startpos<0) {
startpos=0; }
while (true) {
startpos=FindStr(startlabel,startpos); if (startpos==npos) {
break;
}
startpos+=startlabel.length();
startpos=EndLabelPos(startlabel,endlabel,startpos); count++; }
return count; }
bool CLString::IsNum() {
TrimLeft(); TrimRight(); int len=length(); if (len<=0) {
return false; }
TCHAR ch;
for (int i=0;ich=at(i);if (ch==_T('0') || ch==_T('1') || ch==_T('2') || ch==_T('3') || ch==_T('4') || ch==_T('5') || ch==_T('6') || ch==_T('7') || ch==_T('8') || ch==_T('9') ) {
continue; }
return false; } return true; }
bool CLString::IsEmpty()const {
CLString strtemp=c_str(); //换行回车
strtemp.ReplaceStr(_T(\"\\r\"),_T(\"\")); strtemp.ReplaceStr(_T(\"\\n\"),_T(\"\")); strtemp.TrimLeft(); strtemp.TrimRight(); if (strtemp.empty()) {
return true; }
return false; }
// LFile.h: interface for the CLFile class. //
////////////////////////////////////////////////////////////////////// #pragma once #include \"LString.h\"
////////////////////////////////////////////////////////////////////////// //读字符串文件类 //作者:adnlong //日期:20011-12-21
////////////////////////////////////////////////////////////////////////// class CLFile :public CFile {
public: CLFile();
virtual ~CLFile();
//获取一行内容,返回false表示,已经到文件末尾了 //包含换行符\\n
bool ReadString(CLString &str);
//读指定的一段文本,即从当期位置开始读,直到遇到endlabel。 //当hasendlabel为true时,返回值str中包括endlabel
//返回false表示文件末尾了,或者endlabel为空,或者没有找到endlabel bool ReadPause(CLString &str,CLString endlabel,bool hasendlabel=false); //写字符串
void WriteString(const CLString str);
};
// LFile.cpp: implementation of the CLFile class. //
////////////////////////////////////////////////////////////////////// #include \"stdafx.h\" #include \"LFile.h\"
////////////////////////////////////////////////////////////////////// // Construction/Destruction
////////////////////////////////////////////////////////////////////// CLFile::CLFile() { }
CLFile::~CLFile() { }
bool CLFile::ReadString(CLString &str) {
if (GetPosition()==GetLength()) {
return false; }
str.erase();
const TCHAR endchar=_T('\\n'); TCHAR tch;
Read(&tch,sizeof(TCHAR)); while (true) {
str+=tch; if (tch==endchar) {
break; }
if (GetPosition()==GetLength()) {
break; }
Read(&tch,sizeof(TCHAR)); }
return true; }
bool CLFile::ReadPause(CLString &str,CLString endlabel,bool hasendlabel) {
if (GetPosition()==GetLength()) {
return false; }
if (endlabel.empty()) {
return false; }
str.erase();
const int labellen=endlabel.length();
TCHAR *strtemp=new TCHAR[labellen+1]; const TCHAR firstchar=endlabel.at(0); CLString::size_type lastpos=GetPosition(); TCHAR tch;
Read(&tch,sizeof(TCHAR));
while (true) {
if (tch==firstchar) {
memset(strtemp,0,(labellen+1)*sizeof(TCHAR)); Seek(lastpos,CFile::begin);
lastpos=GetPosition();
Read(strtemp,labellen*sizeof(TCHAR)); if (endlabel.compare(strtemp)==0) {
if (hasendlabel) {
str+=strtemp; }else {
Seek(lastpos,CLFile::begin); }
break; }
Seek(lastpos+sizeof(TCHAR),CLFile::begin); }
str+=tch;
if (GetPosition()==GetLength()) {
break; }
lastpos=GetPosition(); Read(&tch,sizeof(TCHAR)); }
delete[] strtemp; return true; }
void CLFile::WriteString(const CLString str) {
if (str.empty()) {
return; }
int size=str.length()*sizeof(TCHAR); Write(str.c_str(),size); } 实现的一个类似MFC中CString类(原创) 收藏 回复 219.149.12.* 这是一个C++课程设计题目 ------------------ 20.VC中CString类的设计 VC中的CString是一个功能强大、灵活的字符串类,它可以实现字符串的输入、输出、运算(+、求长度、截取等)等功能,要求在VC的控制台下做一个CString类实现类似功能。 -------------------- 我实现了它的大部分功能,源代码给大家,不足支持来信告诉我,谢谢! (0) 回复 1楼 2005-12-23 17:15 举报 |个人企业举报垃圾信息举报 219.149.12.* /* * *程序作者: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 #include #include 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*/ 回复 2楼 2005-12-23 17:16 举报 |个人企业举报垃圾信息举报 219.149.12.* /* * *程序作者: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; ipString[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 回复 3楼 2005-12-23 17:17 举报 |个人企业举报垃圾信息举报 219.149.12.* 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;ireturn *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; 回复 4楼 2005-12-23 17:17 举报 |个人企业举报垃圾信息举报 219.149.12.* do{ flag = false;for(int i = 0;i}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;ireturn 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, '\'); return *this; }
//去除对象字符串左侧的位于str所指向的字符串中的字符 CString &CString::TrimLeft(char *str){ for(int i = 0;ireturn *this; } 219.149.12.* //去除对象字符串右侧的换行,空格,制表字符 CString &CString::TrimRight(void){ Trim(2, '\\n'); Trim(2, ' '); Trim(2, '\'); return *this; } ////去除对象字符串右侧的字符ch CString &CString::TrimRight(char ch){ Trim(2, ch); return *this; } //去除对象字符串右侧的位于str所指向的字符串中的字符 CString &CString::TrimRight(char *str){ for(int i = 0;iwhile(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<istream& operator>>(istream &i, CString &Str){ char tmp[256]; i>>tmp; Str = tmp; return i; }
贴吧游戏
万人国战,激情PK尽在电视剧同名游戏《古剑奇谭WEB》 点击参战!
推荐 来自 贴吧游戏 219.149.12.* /* * *程序作者: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 #include #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.\ CString s6(s2, 6, 5);
cout<<\"使用CString()构造函数\"<cout<<\"使用字符串I am harite.构造对象\"<system(\"PAUSE\");cout<<\"使用一个已经存在的对象s2构造新的对象\"<cout<<\"使用字符x重复12次构造对象\"<system(\"PAUSE\");cout<<\"提取字符串houhou~~ I like Music.从第9个索引位置开始共13个字符来构造对象\"<cout<<\"提取已经存在的对象s2\\\"\"<cout<<\"将字符串!!!It is my life...付给对象s1\"<cout<<\"加上字符串\\\"It is a new day\\\"\"<system(\"PAUSE\"); s1+='e';cout<<\"再加上一个字符e\"<cout<<\"从第19个索引开始删除16个字符\"<cout<<\"取上面这个对象字符串的第9个字符\"<system(\"PAUSE\"); s1.SetAt(9, '^');cout<<\"将这个字符变为^\"<cout<<\"去掉其右侧的.\"<cout<<\"删除^字符\"<cout<<\"插入一个m字符\"<cout<<\"从右侧截取此对象13个字符\"<system(\"PAUSE\"); s1.MakeUpper();cout<<\"变为大写字符\"<cout<<\"变为小写字符\"<cout<<\"将i变为I\"<cout<<\"将Is变为is,lIfe变为life\"<cout<<\"将上面那个对象所含有字符串分为3段\"<<\"长度为:\"<tmp = tmp1+tmp2+tmp3;cout<<\"将3个对象用+连接后在=给一个新对象\"<cout<<\"将新对象置空\"<cout<<\"输入一个字符串(别带空格^):\"; cin>>tmp;cout<<\"你输入的字符串为\"<cout<<\"将其反置后为\"<cout<<\"正常显示为\"<cout<<\"其中含有字符串\\\"harite\\\"\"<cout<<\"其中含有字符串\\\"harite\\\"\"<system(\"PAUSE\");cout<<\"使用[]操作符循环显示字符串内容\"<system(\"PAUSE\"); return EXIT_SUCCESS; }回复
7楼
2005-12-23 17:18
举报 |个人企业举报垃圾信息举报 219.149.12.* 郁闷,程序代码格式都被打乱了~~~ 5555555555~~~~~~~~~~ 回复 8楼 2005-12-23 17:21 举报 |个人企业举报垃圾信息举报 594BOSS 亮出16CM 1 上面的程序是自己写的吗,我怎么看着像是MFC库中源程序啊!!�� 回复 9楼 2007-07-13 20:11 举报 |个人企业举报垃圾信息举报 技术动物 亮出21CM 6 if(pString){ delete pString; } 资源泄漏 如果是原创,还是鼓励一下. MFC 的CString是用引用计数之实现的. 效率会更一些. LZ的不支持UNICODE 的.