//.h
#pragma once
#include<string.h>
#include<iostream>
using namespace std;
class CString
{
char* m_pCString;
int m_nLenth;
public:
~CString();
CString();//无参的构造
CString(const char*p);//有参构造之字符串定义对象
CString(const CString& str);//有参构造之对象定义对象
CString(char ch, int nRepeat = 1);//有参构造之定义N个相同字符
CString(const char*p, int nLenth);//有参构造之字符串一部分定义对象
operator const char*()//转换类型重载函数
{
return m_pCString;
}
char operator [](int indext)const;//重载[]
CString&operator =(const char*p);//赋值之字符串赋值
CString&operator =(const CString& str);//赋值之对象赋值
CString& operator +=(const CString&str);//重载+=之对象
CString& operator +=(const char*p);//重载+=之字符串
friend CString operator +(const CString& str1, const CString& str2);//重载+
friend ostream& operator <<(ostream& cout, CString& str);//重载ostream::cout的<<符号,自定义输出对象
friend bool operator !=(const CString&str1, const CString&str2);
friend bool operator !=(const CString&str1, const char*p);
friend bool operator !=(const char*p, const CString&str2);
friend bool operator ==(const CString&str1, const CString&str2);
friend bool operator ==(const CString&str1, const char*p);
friend bool operator ==(const char*p, const CString&str2);
friend bool operator < (const CString&str1, const CString&str2);
friend bool operator < (const CString&str1, const char*p);
friend bool operator < (const char*p, const CString&str2);
friend bool operator >(const CString&str1, const CString&str2);
friend bool operator >(const CString&str1, const char*p);
friend bool operator >(const char*p, const CString&str2);
int GetLenth()const
{
return m_nLenth;
}
int Find(const char*pszSub, int iStrat = 0);
int Find(char ch, int iStrat = 0);
CString&MakeLower();
CString&MakeUpper();
CString&MakeReverse();
CString Mid(int iFirst, int nCount)const;
CString Mid(int iFirst)const;
CString Left(int nCount)const;
CString Right(int nCount)const;
};
//.cpp
#define _CRT_SECURE_NO_WARNINGS
#include "String.h"
CString::CString():m_nLenth(0)//无参的构造
{
m_pCString = new char(0);
}
CString::CString(const char * p)//有参构造之字符串定义对象
{
int lenth = (int)strlen(p);
m_pCString = new char[lenth + 1];
strcpy(m_pCString, p);
m_nLenth = lenth;
}
CString::CString(const CString& str)//有参构造之对象定义对象
{
int lenth = str.GetLenth();
m_pCString = new char[lenth + 1];
strcpy(m_pCString, str.m_pCString);
m_nLenth = lenth;
}
CString::CString(char ch, int nRepeat)//有参构造之定义N个相同字符对象
{
m_pCString = new char[nRepeat + 1];
memset(m_pCString, ch, nRepeat);
m_pCString[nRepeat] = '\0';
m_nLenth = nRepeat;
}
CString::CString(const char * p, int nLenth)//有参构造之字符串一部分定义对象
{
int min=nLenth;
if (min > (int)strlen(p))
min = strlen(p);
this->m_pCString = new char[min + 1];
memcpy(m_pCString, p, min);
m_pCString[min] = '\0';
this->m_nLenth = min;
}
CString::~CString()
{
delete[] m_pCString;
m_nLenth = 0;
}
CString & CString::operator=(const char * p)//赋值之字符串赋值
{
int lenth = (int)strlen(p);
if (lenth > this->GetLenth())
{
delete[]this->m_pCString;//定义之后的赋值要把m_pCString指向的内存删除
this->m_pCString = new char[lenth + 1];
}
strcpy(this->m_pCString, p);
m_nLenth = lenth;
return *this;
}
CString & CString::operator=(const CString & str)//赋值之对象赋值
{
int lenth = str.GetLenth();
if (lenth > this->GetLenth())
{
delete[]this->m_pCString;//定义之后的赋值要把m_pCString指向的内存删除
this->m_pCString = new char[lenth + 1];
}
strcpy(this->m_pCString, str.m_pCString);
m_nLenth = lenth;
return *this;
}
CString& CString::operator +=(const CString & str)//重载+=
{
int lenth = str.GetLenth()+this->GetLenth();
char*p = new char[lenth + 1];
strcpy(p, this->m_pCString);
strcat(p, str.m_pCString);
delete[]this->m_pCString;
this->m_pCString = p;
/*strcat(this->m_pCString, str.m_pCString);*/
this->m_nLenth = lenth;
return *this;
}
CString & CString::operator+=(const char * p)
{
int lenth = strlen(p) + this->GetLenth();
char*ptemp = new char[lenth + 1];
strcpy(ptemp, this->m_pCString);
strcat(ptemp, p);
delete[]this->m_pCString;
this->m_pCString = ptemp;
m_nLenth = lenth;
return *this;
}
char CString::operator[](int indext)const
{
if (indext > m_nLenth)
return '\0';
return m_pCString[indext];
}
//...
CString operator+(const CString& str1, const CString& str2)//重载+
{
CString str;
int lenth = str1.GetLenth() + str2.GetLenth();
delete[]str.m_pCString;
str.m_pCString = new char[lenth + 1];
strcpy(str.m_pCString, str1.m_pCString);
strcat(str.m_pCString, str2.m_pCString);
str.m_nLenth = lenth;
return str;
}
ostream& operator <<(ostream& cout, CString& str)//重载ostream::cout的<<符号,自定义输出对象
{
cout << str.m_pCString;
return cout;
}
bool operator!=(const CString & str1, const CString & str2)
{
return strcmp(str1.m_pCString, str2.m_pCString) != 0;
}
bool operator!=(const CString & str1, const char * p)
{
return strcmp(str1.m_pCString, p) != 0;
}
bool operator!=(const char * p, const CString & str2)
{
return strcmp(p, str2.m_pCString) != 0;
}
bool operator==(const CString & str1, const CString & str2)
{
return strcmp(str1.m_pCString, str2.m_pCString) == 0;
}
bool operator==(const CString & str1, const char * p)
{
return strcmp(str1.m_pCString, p) == 0;
}
bool operator==(const char * p, const CString & str2)
{
return strcmp(p, str2.m_pCString) == 0;
}
bool operator<(const CString & str1, const CString & str2)
{
return strcmp(str1.m_pCString, str2.m_pCString) < 0;
}
bool operator<(const CString & str1, const char * p)
{
return strcmp(str1.m_pCString, p) < 0;
}
bool operator<(const char * p, const CString & str2)
{
return strcmp(p, str2.m_pCString) < 0;
}
bool operator>(const CString & str1, const CString & str2)
{
return strcmp(str1.m_pCString, str2.m_pCString) > 0;
}
bool operator>(const CString & str1, const char * p)
{
return strcmp(str1.m_pCString, p) > 0;
}
bool operator>(const char * p, const CString & str2)
{
return strcmp(p, str2.m_pCString) > 0;
}
int CString::Find(const char * pszSub, int iStrat)
{
char*p = m_pCString;
if(iStrat > m_nLenth - 1)
return -1;
char*s = strstr(p + iStrat, pszSub);//利用strstr找字符
if (s == NULL)
return -1;
return s - p;
}
int CString::Find(char ch, int iStrat)
{
char*p = m_pCString;
if (iStrat > m_nLenth - 1)
return -1;
char* s = strchr( p + iStrat, ch);//利用strchr找字符
if (s == NULL)
return -1;
return s - p;
}
CString & CString::MakeLower()
{
int i = 0;
while (m_pCString[i])
{
if (m_pCString[i] >= 'A' && m_pCString[i] <= 'Z')
m_pCString[i] += 32;
++i;
}
return *this;
}
CString & CString::MakeUpper()
{
int i = 0;
while (m_pCString[i])
{
if (m_pCString[i] >= 'a' && m_pCString[i] <= 'z')
m_pCString[i] -= 32;
++i;
}
return *this;
}
CString & CString::MakeReverse()
{
int lenth = m_nLenth;
char*p = new char[lenth + 1];
int i = 0;
while (i<lenth)
{
p[i] = m_pCString[m_nLenth - i - 1];
++i;
}
p[lenth] = '\0';
delete[]m_pCString;
m_pCString = p;
return *this;
}
CString CString::Mid(int iFirst, int nCount) const
{
char*p = m_pCString;
CString str(p + iFirst, nCount);//利用构造函数构造出str
return str;
}
CString CString::Mid(int iFirst) const
{
char*p = m_pCString;
CString str(p + iFirst);//利用构造函数构造出str
return str;
}
CString CString::Left(int nCount) const
{
char*p = m_pCString;
CString str(p, nCount);
return str;
}
CString CString::Right(int nCount) const
{
char*p = m_pCString+m_nLenth;//p指向‘0’
CString str(p-nCount, nCount);
return str;
}
源码下载地址:https://download.csdn.net/download/aidehua88/10333808