我给你一个我创建的,并且非常强大的STRING类。包括两个文件一个头文件另一个是CPP文件,main()方法中教你怎么使用。
//头文件保存Cpp2.h
#include
#include
class STRING
{
private:
char* m_str;
public:
STRING(void); //默认构造函数
STRING(char *str); //带参数构造函数
STRING(const STRING &str); //复制构造函数
~STRING(void); //析构函数
STRING& operator = (const STRING& other); //字符串复制
STRING& operator = (const char* str); //字符串赋值
STRING& operator + (const STRING& other); //字符串连接
STRING& operator + (const char *str); //字符串连接
bool operator == (const STRING& other) const; //字符串比较
bool operator == (const char* str) const; //字符串比较
char& operator [](int i); //取字符串位
char* GetBuffer(void); //字符串
int GetLen(void); //取长度
char* GetLeft(int len); //从左边取长度为len的字符串
char* GetRight(int len); //从右边取长度为len的字符串
char* SubString(int len, int start= 0); //从start位置取长度len的字符串
int GetIndex(char*search, int start = 0); //字符串搜索,返回第一个子串在m_str中的位置,从0开始数
int Replace(char *search, char *replace); //search被替换的字符串, replace替换的字符串, 返回被替换的次数
void Reverse(void); //字符串反转
void print(void); //打印字符串
};
//CPP文件保存名为Cpp2.cpp
#include
#include
#include
#include
#include "Cpp2.h"
//默认构造函数
STRING::STRING(void)
{
m_str = new char('\0');
}
//带参数构造函数
STRING::STRING(char *str)
{
if(m_str != NULL)
{
m_str = new char[strlen(str) + 1];
strcpy(m_str, str);
}
else
{
m_str = new char[1];
*m_str = '\0';
}
}
//copy构造函数
STRING::STRING(const STRING &str)
{
m_str = new char[strlen(str.m_str) + 1];
if(m_str) strcpy(m_str, str.m_str);
}
//析构函数
STRING::~STRING()
{
if(m_str != NULL) delete [] m_str;
cout << "~STRING() is called" << endl;
}
//赋值运算,字符串复制
STRING& STRING::operator = (const STRING &other)
{
if(this != &other)// 如果str不是它本身
{
if(m_str != NULL) delete [] m_str; //防止内存泄露
m_str = new char[strlen(other.m_str) + 1];
strcpy(m_str, other.m_str);
}
return *this;
}
//字符串复制
STRING& STRING::operator = (const char* str)
{
if(m_str != NULL) delete [] m_str;
m_str = new char[strlen(str) + 1];
strcpy(m_str, str);
return *this;
}
//加法运算,字符串连接
STRING& STRING::operator + (const STRING& other)
{
char *temp = new char[strlen(m_str) + strlen(other.m_str) + 1];
strcpy(temp, m_str);
delete [] m_str;
strcat(temp, other.m_str);
m_str = temp;
return *this;
}
//加法运算,字符串连接
STRING& STRING::operator + (const char *str)
{
char *q = new char[strlen(m_str) + strlen(str) + 1];
strcpy(q, m_str);
delete [] m_str;
strcat(q, str);
m_str = q;
return *this;
}
//字符串比较
bool STRING::operator == (const char* str) const
{
return strcmp(m_str, str) == 0;
}
//类与类比较
bool STRING::operator == (const STRING& other) const
{
if(this == &other) return true; // this 指针指向 other 对象
return strcmp(m_str, other.m_str) == 0;
}
//[]操作符重载,取下标为i的字符
char& STRING::operator [](int i)
{
return this->m_str[i];
}
//取字符串
char* STRING::GetBuffer(void)
{
return this->m_str;
}
//取长度
int STRING::GetLen(void)
{
return strlen(m_str);
}
//从左边取长度为len的字符串
char* STRING::GetLeft(int len)
{
if(len > GetLen()) return this->m_str;
char*strtmp = (char * )malloc(sizeof(char) * (len + 1));
char *p = strtmp;
memset(strtmp, 0x00, len + 1);
for(int i = 0 ; i < len; i++)
{
*p++ = m_str[i];
}
*p = '\0';
return strtmp;
}
//从右边取长度为len的字符串
char* STRING::GetRight(int len)
{
if(len > GetLen()) return this->m_str;
char*strtmp = (char * )malloc(sizeof(char) * (len + 1));
memset(strtmp, 0x00, len + 1);
char *p = strtmp + len;
*p = '\0';
for(int i = GetLen() - 1 ; i > 0 ; i--)
{
*(--p) = m_str[i];
}
return strtmp;
}
//从start位置取长度len的字符串
char* STRING::SubString(int start= 0, int len)
{
return NULL;
}
//字符串搜索,返回子串substr在m_str第start个字符之后的位置,若不存在,返回-1
int STRING::GetIndex(char *search, int start)
{
if (start < 0) return -1;
int i = start, j = 0;
int searchlen = strlen(search);
if(GetLen() - start < searchlen) return -1;
while(i < GetLen() && j < searchlen)
{
if(m_str[i] == search[j])
{
i++;
j++;
}
else
{
i = i - j + 1;
j = 0;
}
}
if(j > searchlen - 1)
return i - searchlen;
else
return -1;
}
//字符串替换
int STRING::Replace(char *search, char *replace)
{
int count = 0;
int index, start = 0;
int len = strlen(search);
STRING strep;
//在目标串能找到替换串
while( (index = GetIndex(search, start)) > 0)
{
strep = GetLeft(index);//取左边的字符串
strep = strep + replace;//连接替换的字符串
strep = strep + GetRight(GetLen() - index - strlen(search));//连接被替换之后的字符
start = index + strlen(replace);//设置下一个搜索位置
*this = strep;
count++;//替换的次数增加1
}
return count;
}
//字符串反转
void STRING::Reverse(void)
{
char *temp, *p, *q;
int count = strlen(m_str);
temp = new char[strlen(m_str) + 1];
q = temp;
p = &m_str[count - 1];
while(count-- > 0) *q++ = *p--;
*q = '\0';
delete []m_str;
m_str = temp;
}
//打印字符串
void STRING::print(void)
{
char *p = m_str;
while(*p != '\0') cout << *p++;
cout<
void main()
{
STRING ss = "ababcabcdabc";
char*p = "abc";
int count = ss.Replace("abc", "1234");
printf("替换%d次, 替换后字符串:%s\n", count, ss.GetBuffer());
getchar();
}
指定串的查找,
指定串用数组a存放,
遍历目标串,将每一个值与a[0]比较,
不相等:继续遍历
相等:
进入内部循环(从数组a的第一个成员和当前指针指向的字符串的成员开始向后遍历),
-如果中途一旦发现有字符不等,break退出,从当前指向目标串的指针的下一个字符继续遍历
-当遍历到数组a结束时,都相等,输出,并将当前指向目标串的指针跳过sizeof(a)的长度后继续遍历
指定串的替换
在上面的查找的基础上,
-当遍历到数组a结束时,都相等,将目标串的当前指针的前sizeof(a)的成员到当前指向成员替换成新的字符
//用标准库,很简单,一看就明
#include
#include
#include
using namespace std;
int main()
{
int strMax = 0;
string str;
cout << "字符串数目: " << ends;
cin >> strMax;
vector
for (int i = 0; i != strMax; ++i)
{
cin >> str;
vecStr.push_back(str);
}
cout << "查找串: " << ends;
cin >> str;
bool sign = false;
vector
for (iter = vecStr.begin(); iter != vecStr.end(); ++iter)
{
if (*iter == str)
{
cout << "串 " << str << "找到." << endl;
cout << "替换串为: " << ends;
cin >> str;
*iter = str;
sign = true;
}
}
if (sign == false)
cout << "找不到串: " << str << endl;
else
for (iter = vecStr.begin(); iter != vecStr.end(); ++iter)
{
cout << *iter << endl;
}
return 0;
}
#include
#include
using namespace std;
int main()
{
string str = "abdaetglljlj";/////指定串,可根据要求替换
string substr = "lj";////要查找的串,可根据要求替换
int pos;
pos = str.find(substr);////查找指定的串
while (pos != -1)
{
str.replace(pos,substr.length(),"aa");////用新的串替换掉指定的串
pos = str.find(substr);//////继续查找指定的串,直到所有的都找到为止
}
cout<
}
#include
using namespace std;
int GetStringPos(char* str1, char* str2 )
{
int j=0;
for(int i=0; i
if(str1[i]==str2[j])
{
j++;
if(j==strlen(str2))
return i-strlen(str2)+1;
}
else
j=0;
}
return -1; //在母字符串找不到子字符串
}
void Replace(char* str, char *find, char *replace)
{
char* pStr = new char[strlen(str)];
int nIndex = 0;
while(1)
{
nIndex = GetStringPos(str, find); //获取要查找字符串的位置
if(nIndex == -1) break;
strcpy(pStr, nIndex + str + strlen(find));
strcpy(nIndex + str, replace);
strcpy(nIndex + str + strlen(replace), pStr);
}
delete[] pStr;
}
int main()
{
char str[100] = "CIW__BLUE";
Replace(str, "_", "ABC");
cout<
}