#include<iostream>
#include<cstring>
using namespace std;
class String{
private:
char *pstr;
public:
String(const char *str = " ")
{
pstr = new char[strlen(str) + 1];
strcpy(pstr, str);
}
String(const String&s) //复制构造函数
{
pstr = new char[strlen(s.pstr) + 1];
strcpy(pstr, s.pstr);
}
void show()
{
cout << pstr << endl;
}
friend ostream &operator<<(ostream &os, String&s); //重载输出运算符<< 1
friend istream &operator>>(istream &is, String&s);
friend bool operator==(const String &s1, const String &s2);//比较两个字符串是否相等
String &operator=(String &s); //重载赋值运算符 3
String &operator+(String &s); //重载+运算符连接
String &operator+=(const String &s);//把字符串s连接到当前字符串的结尾
char &operator[](int n);
int length() //获取字符串的长度 4
{
return strlen(pstr);
}
bool isempty() //当前字符串是否为空
{
return length() == 0 ? true : false;
};
bool operator>(const String&s);
bool operator<(const String&s);
bool operator>=(const String&s);
bool operator<=(const String&s);
bool operator!=(const String&s);
int find_first_of(char c);//从前向后查找c出现的第一个位置
int find_last_of(char c);//从后向前查找c出现的第一个位置
int find_s_first_of(const char *s);//从前向后查找字符串s第一次出现的位置
int find_s_last_of(const char *s);//从后向前查找字符串s第一次出现的位置
int find(char s, int pos); //从pos开始查找字符c在字符串中的位置
int rfind(char s, int pos); //从pos开始逆序查找字符c在字符串中的位置
int find(char *s, int pos); //从pos开始查找字符串c在字符串中的位置
int rfind(char *s, int pos); //从pos开始逆序查找字符串c在字符串中的位置
String &exchange(char s1, char s2); //将字符串中所有的字符s1都用字符s2替换
String &exchange(char *s1, char *s2); //将字串中的字符串s1替换成字符串s2
void swap(String& s); //交换两个字符串的值
String &Toupper(); //将小写化为大写
String &Tolpper(); //大写变小写
String substr(int pos, int n); //从pos开始返回n个字符组成的字串
String &deletes(char c); //删除字符串中所有的字符c
~String(){ delete[]pstr; };
};
#include "String.h"
//重载输出运算符
ostream &operator<<(ostream &os, String &s)
{
os << s.pstr;
return os;
}
//重载输入运算符
istream &operator>>(istream &is, String &s)
{
is >> s.pstr;
return is;
}
//重载+运算符连接
String &String::operator+(String &s)
{
strcat(this->pstr, s.pstr);
return *this;
}
//重载赋值运算符
String &String::operator=(String &s)
{
if (this == &s)
return *this;
delete[] pstr;
int length = strlen(s.pstr);
pstr = new char[length + 1];
strcpy(pstr, s.pstr);
return *this;
}
//把字符串s连接到当前字符串的结尾
String &String::operator+=(const String &s)
{
strcat(this->pstr, s.pstr);
return *this;
}
char &String::operator[](int n)
{
char s = '\0';
if (n <strlen(pstr) && n>0)
return pstr[n];
else
return s;
}
//比较两个字符串是否相等
bool operator==(const String &s1, const String &s2)
{
int i;
for (i = 0; s1.pstr[i] != '\0'&&s2.pstr != '\0'; i++)
{
if (s1.pstr[i] == s2.pstr[i])
continue;
else
return false;
}
}
bool String::operator>(const String&s)
{
return strcmp(this->pstr, s.pstr) > 0;
}
bool String::operator<(const String&s)
{
return strcmp(this->pstr, s.pstr) < 0;
}
bool String::operator>=(const String&s)
{
return strcmp(this->pstr, s.pstr) >= 0;
}
bool String::operator<=(const String&s)
{
return strcmp(this->pstr, s.pstr) <= 0;
}
bool String::operator!=(const String&s)
{
if (strcmp(pstr, s.pstr) == 0)
return false;
else
return true;
}
String &String::Toupper()
{
for (int i = 0; pstr[i] != 0; i++)
{
if (pstr[i] >= 'a' && pstr[i] <= 'z')
pstr[i] = pstr[i] - 32;
}
return *this;
}
String &String::Tolpper()
{
strlwr(this->pstr);
return *this;
}
int String::find_first_of(char c)
{
int index = -1;
for (int i = 0; pstr[i] != '\0'; i++)
{
if (pstr[i] == c)
{
index = i;
break;
}
}
return index;
}
int String::find_last_of(char c)
{
int index = 0;
for (int i = 0; pstr[i] != '\0'; i++)
{
if (pstr[i] == c)
index = i;
}
return index;
}
int String::find_s_first_of(const char *s)
{
int index = -1;
for (int i = 0; pstr[i]; i++)
{
int j;
for (j = i; pstr[j] && s[j - i]; j++)
{
if (pstr[j] != s[j - i])
break;
}
if (s[j - i] == '\0')
{
index = i;
break;
}
}
return index;
}
int String::find_s_last_of(const char *s)
{
int index = -1;
for (int i = strlen(pstr) - 1; i >= strlen(s); i--)
{
//printf("i=%d \n",i);
int j;
for (j = i - strlen(s) + 1; j <= i; j++)
{
//printf("j=%d \n", j);
if (j < 0)
break;
if (pstr[j] != s[j + strlen(s) - i - 1])
{
//printf("pstr[%d] =%c s[%d + %d - %d - 1] =%c \n", j, pstr[j], j, strlen(s), i, s[j + strlen(s) - i - 1]);
break;
}
}
if (j == i + 1)
{
index = i - strlen(s) + 1;
//printf("index=%d",index);
break;
}
}
return index;
}
int String::find(char s, int pos)
{
int index = -1;
for (int i = pos; pstr[i]; i++)
{
if (pstr[i] == s)
{
index = i;
break;
}
}
return index;
}
int String::rfind(char s, int pos)
{
int index = -1;
for (int i = pos - 1; i >= 0; i--)
{
if (pstr[i] == s)
{
index = i;
break;
}
}
return index;
}
int String::find(char *s, int pos)
{
int index = -1;
for (int i = pos; pstr[i]; i++)
{
int j;
for (j = i; pstr[j] && s[j - i]; j++)
{
if (pstr[j] != s[j - i])
break;
}
if (s[j - i] == '\0')
{
index = i;
break;
}
}
return index;
}
int String::rfind(char *s, int pos)
{
int index = -1;
for (int i = pos - 1; i >= strlen(s); i--)
{
int j;
for (j = i - strlen(s) + 1; j <= i; j++)
{
if (j < 0)
break;
if (pstr[j] != s[j + strlen(s) - 1 - i])
break;
}
if (j == i + 1)
{
index = i - strlen(s) + 1;
break;
}
}
return index;
}
String &String::exchange(char s1, char s2)
{
for (int i = 0; this->pstr[i] != '\0'; i++)
{
if (this->pstr[i] == s1)
this->pstr[i] = s2;
}
return *this;
}
String &String::exchange(char *s1, char *s2)
{
return *this;
}
void String::swap(String& s)
{
char * p = pstr;
pstr = s.pstr;
s.pstr = p;
}
String String::substr(int pos, int n)
{
char *p = new char[n + 1];
for (int i = 0; i < n; i++)
*p++ = this->pstr[pos + i];
return String(p);
}
String &String::deletes(char c)
{
for (int i = 0; this->pstr[i] != '\0'; i++)
{
if (this->pstr[i] == c)
{
for (int j = i; this->pstr[i] != '\0'; j++)
{
this->pstr[j] = this->pstr[j + 1];
if (this->pstr[j + 1] == '\0')
break;
}
}
}
return *this;
}
int main(){
String b, a("abc");
String s1 = ("012355");
String s2 = ("123abs456abc");
cout << "请输入b的值:";
cin >> b;
cout << "b=" << b << endl;
cout << "a=" << a << endl;
a = b;
cout << "a=" << a << endl;
cout << "a.length()=" << a.length() << endl;
cout << "s1=" << s1 << endl;
cout << "s1+a=" << s1 + a << endl;
cout << "s1=" << s1 << endl;
s1 += a;
cout << "s1=" << s1 << endl;
cout << "s2[3]=" << s2[3] << endl;
if (b.isempty())
cout << "b为空" << endl;
else
cout << "b不为空" << endl;
if (a == s1)
cout << "a=s1" << endl;
else
cout << "a!=s1" << endl;
if (a > s1)
cout << "a>s1" << endl;
else
cout << "a<=s1" << endl;
if (a < s1)
cout << "a<s1" << endl;
else
cout << "a>=s1" << endl;
a = s2;
cout << "a=" << a << endl;
cout << "s2=" << s2 << endl;
if (a >= s2)
cout << "a>=s2" << endl;
else
cout << "a<s2" << endl;
if (a <= s1)
cout << "a<=s1" << endl;
else
cout << "a>s1" << endl;
if (a != s2)
cout << "a!=s2" << endl;
else
cout << "a=s2" << endl;
cout << "s1=" << s1 << endl;
cout << s1.Toupper() << endl;
cout << s1.Tolpper() << endl;
String s3("012345abcde");
cout << "s3 \t\t\t= " << s3 << endl;
cout << "s3.find_first_of('a')\t\t=" << s3.find_first_of('a') << endl;
cout << "s3.find_last_of('e') \t\t=" << s3.find_last_of('e') << endl;
cout << "s3.find_s_first_of(\"bc\")\t= " << s3.find_s_first_of("bc") << endl;
cout << "s3.find_s_last_of(\"123\")\t= " << s3.find_s_last_of("123") << endl;
cout << "s3.find('e', 6)\t\t\t= " << s3.find('e', 6) << endl;
cout << "s3.rfind('e', 6)\t\t=" << s3.rfind('e', 6) << endl;
cout << "s3.find(\"abc\", 5)\t\t=" << s3.find("abc", 5) << endl;
cout << "s3.rfind(\"cd\", 5)\t\t=" << s3.rfind("cd", 5) << endl;
cout << "s3.exchange('b', 'X')\t\t=" << s3.exchange('b', 'X') << endl;
cout << "s3.exchange(\"123\",\"555\")\t=" << s3.exchange("123", "555") << endl;
cout << "s3.deletes('5')\t\t\t=" << s3.deletes('5') << endl;
getchar();
return 0;
}
字符串查找
void GSTestMaplotlib::GetStringOfSub(std::string src, std::string splitStr, std::vector<std::string>& strVec, int isRFind)
{
if (isRFind == 0) //反向查找
{
int pos = src.rfind(splitStr);
if (pos != -1)
{
std::string dstStr = src.substr(pos, src.length() - 1);
strVec.emplace_back(dstStr);
std::string srcStr = src.substr(0, pos);
GetStringOfSub(srcStr, splitStr, strVec, isRFind);
}
else
{
strVec.emplace_back(src);
}
}
else
{
int pos = src.find(splitStr);
if (pos != -1)
{
std::string dstStr = src.substr(0, pos);
strVec.emplace_back(dstStr);
std::string srcStr = src.substr(pos + 1, src.length());
GetStringOfSub(srcStr, splitStr, strVec, isRFind);
}
else
{
strVec.emplace_back(src);
}
}
}