一、string简介
C++ 中,std::string 是用于处理字符串的标准库类。它提供了一系列成员函数和操作符,使得字符串的操作更加方便和灵活。
string与char*的区别:
- char* 是一个指针
- string本质上是一个类,类的内部封装了char*,即string是一个char*型的容器
- string管理char*所分配的内存,不用担心复制越界和取值越界等
二、string构造函数
示例:
#include <iostream>
#include <string>
using namespace std;
void test01()
{
string s1; //创建空字符串,调用无参构造函数
string s2("hello C++"); //把const char*转换成了string
string s3(s2); //调用拷贝构造函数,使用s2初始化s3
string s4(10, 'c');
cout << "s1 = " << s1 << endl;
cout << "s2 = " << s2 << endl;
cout << "s3 = " << s3 << endl;
cout << "s4 = " << s4 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
三、string字符串的拼接
示例:
#include <iostream>
#include <string>
using namespace std;
void test01()
{
string s1 = "AB";
s1 += "CD"; //第1种拼接方法:+=char*字符串
cout << "s1 = " << s1 << endl;
s1 += 'e'; //第2种拼接方法:+=字符
cout << "s1 = " << s1 << endl;
string s0 = "FG";
s1 += s0; //第3种拼接方法;+=string字符串
cout << "s1 = " << s1 << endl;
string s2 = "AB";
s2.append("CD"); //第4种拼接方法;append("");
cout << "s2 = " << s2 << endl;
s2.append("EFGH",3); //第5种拼接方法;append("",n);
cout << "s2 = " << s2 << endl;
string s3 = "HIJ";
s2.append(s3); //第6种拼接方法;append(string);
cout << "s2 = " << s2 << endl;
string s4 = "KLMNOPQ";
s2.append(s4,0,5); //第7种拼接方法;append(string,p,n);
cout << "s2 = " << s2 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
四、string查找与替换
注意:
find
查找是从左向后,rfind
从右向左find
与rfind
找到指定字符串时,返回查找的第一个字符的位置下标;未找到则返回-1replace
在替换时,要指定起始位置p,替换字符数量n,替换后的字符串s
示例:
#include <iostream>
#include <string>
using namespace std;
void test01()
{
//查找
string s1 = "ABCDEFGHIGK";
int p = s1.find("EF");
if (p == -1)
{
cout << "not find!" << endl;
}
else
{
cout << "find p = " << p << endl;
}
p = s1.rfind("EF");
if (p == -1)
{
cout << "not find!" << endl;
}
else
{
cout << "rfind p = " << p << endl;
}
}
void test02()
{
//替换
string s1 = "ABCDEF";
s1.replace(1, 3, "0000");
cout << "s1 = " << s1 << endl;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
五、字符串比较
注意:
- 字符串比较是按字符的ASCII码进行对比(= 返回 0) (> 返回 1) (< 返回 -1)
- 字符串对比主要是用于比较两个字符串是否相等,判断谁大谁小的意义不大
示例:
#include <iostream>
#include <string>
using namespace std;
void test01()
{
string s1 = "aaaaa";
string s2 = "AAAAA";
int ret = s1.compare(s2);
if (ret == 0)
{
cout << "s1 等于 s2" << endl;
}
else if (ret > 0)
{
cout << "s1 大于 s2" << endl;
}
else
{
cout << "s1 小于 s2" << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
六、string 字符获取、插入、删除
示例:
#include <iostream>
#include <string>
using namespace std;
void test01()
{
string s = "ABCDEFG";
cout << "s[1] = " << s[1] << endl;
//修改s的第二个字符
s.at(1) = 'X';
cout << "s.at(1)" << s.at(1) << endl;
//插入字符串
s.insert(1, "555");
cout << "s.insert = " << s << endl;
//删除字符串
s.erase(1, 3);
cout << "s.erase = " << s << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
七、string子串
#include <iostream>
#include <string>
using namespace std;
void test01()
{
string s1 = "ABCDEFG";
//截取子字符串
string s2 = s1.substr(1,4);
cout << "s2 = " << s2 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}