1.string基本概念
本质:
string本质是c++风格的字符串,而string本质上是一个类
string和char*区别:
char*是一个指针
string是一个类,类内部封装了char*,管理这个字符串,是一个char*型的容器
特点:
string类内部封装了很多成员方法
例如:查找find,拷贝copy,删除delete替换replace,插入insert
string管理char*所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责
2.string构造函数
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm> //标准算法头文件
#include<numeric>
#include<map>
#include<set>
#include<utility>
#include<fstream>
#include<string>
//string的构造函数
void test01() {
string s1; //默认构造
const char* str = "hello world";
string s2(str);
cout << "s2= " << s2<<endl;
string s3(s2);
cout << "s3= " << s3 << endl;
string s4(10, 'a');
cout << "s4= " << s4 << endl;
}
int main(int argc,char**argv) {
test01();
system("pause");
return 0;
}
3.string赋值操作
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm> //标准算法头文件
#include<numeric>
#include<map>
#include<set>
#include<utility>
#include<fstream>
#include<string>
void test01() {
string str1;
str1 = "hello world";
cout << "str1= " << str1<<endl;
string str2;
str2 = str1;
cout << "str2= " << str2 << endl;
string str3;
str3 = 'a';
cout << "str3= " << str3 << endl;
string str4;
str4.assign("hello c++");
cout << "str4= " << str4 << endl;
string str5;
str5.assign("hello c++", 5);
cout << "str5= " << str5 << endl;
string str6;
str6.assign(str5);
cout << "str6= " << str6 << endl;
string str7;
str7.assign(10, 'w');
cout << "str7= " << str7 << endl;
}
int main(int argc,char**argv) {
test01();
system("pause");
return 0;
}
4.stirng字符串拼接
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm> //标准算法头文件
#include<numeric>
#include<map>
#include<set>
#include<utility>
#include<fstream>
#include<string>
void test01() {
string str1="我";
str1 += "想去腾讯";
cout << "str1= " << str1<<endl;
string str2;
str2 = str1;
cout << "str2= " << str2 << endl;
string str3="I";
str3.append(" love ");
cout << "str3= " << str3 << endl;
str3.append("game acc", 4);
cout << "str3= " << str3 << endl;
str3.append(str2);
cout << "str3= " << str3 << endl;
str3.append(str2, 0, 6); //参数2从哪个位置开始截取,参数3是截取的字符个数
cout << "str3= " << str3 << endl;
}
int main(int argc,char**argv) {
test01();
system("pause");
return 0;
}
5.string查找和替换
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm> //标准算法头文件
#include<numeric>
#include<map>
#include<set>
#include<utility>
#include<fstream>
#include<string>
//1.查找
void test01() {
string str1 = "abcdefg";
int pos=str1.find("cd"); //find返回一个int ,用pos接收一下
cout << "pos= " << pos << endl; //输出2,从0开始计数,c是下标2,就返回2 如果是dc(即不存在),则返回-1
if (pos == -1) {
cout << "未找到字符串" << endl;
}
else {
cout << "找到字符串,pos= " << pos << endl;
}
//rfind
//rfind是从右往左查找 find是从左往右查找
pos = str1.rfind("cd");
cout << "pos= " << pos << endl;
}
//2.替换
void test02() {
string str1 = "abcdefg";
//从1号位置起,把3个字符替换成1111
str1.replace(1, 3, "1111");
cout << "str1 = " << str1 << endl;
}
int main(int argc,char**argv) {
test01();
test02();
system("pause");
return 0;
}
6.string字符串比较
比较方式:ASCⅡ码
= 返回0
> 返回1
< 返回-1
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm> //标准算法头文件
#include<numeric>
#include<map>
#include<set>
#include<utility>
#include<fstream>
#include<string>
void test01() {
string s1 = "hello";
string s2 = "hello";
if (s1.compare(s2) == 0) {
cout << "str1=str2" << endl;
}
//主要比较相等,判断大小意义不大
}
int main(int argc,char**argv) {
test01();
system("pause");
return 0;
}
7.string字符存取
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm> //标准算法头文件
#include<numeric>
#include<map>
#include<set>
#include<utility>
#include<fstream>
#include<string>
void test01() {
string s1 = "hello";
//1.通过[]访问单个字符
for (int i = 0; i < s1.size(); i++) {
cout << s1[i] << " ";
}
cout << endl;
//2.通过at方式访问单个字符
for (int i = 0; i < s1.size(); i++) {
cout << s1.at(i) << " ";
}
cout << endl;
//字符修改
s1[0] = 'x';
s1.at(1) = 'x';
}
int main(int argc,char**argv) {
test01();
system("pause");
return 0;
}
8.string的插入和删除
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm> //标准算法头文件
#include<numeric>
#include<map>
#include<set>
#include<utility>
#include<fstream>
#include<string>
void test01() {
string s1 = "hello";
//插入
s1.insert(1, "111"); //插入删除下标都是从0开始, 1下标对应的e,就在e之前插入
cout << "s1= " << s1 << endl;
//删除
s1.erase(1, 3);
cout << "s1= " << s1 << endl;
}
int main(int argc,char**argv) {
test01();
system("pause");
return 0;
}
9.string子串
从字符串中获取想要的字串
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm> //标准算法头文件
#include<numeric>
#include<map>
#include<set>
#include<utility>
#include<fstream>
#include<string>
void test01() {
string s1 = "abcde";
string s2 = s1.substr(1, 3);
cout << "s2= " << s2 << endl; //输出bcd
}
//实用操作
void test02() {
string s1 = "xujilei@123.com";
int pos = s1.find("@");
string s2 = s1.substr(0, pos);
cout << "用户名为:" << s2 << endl;
}
int main(int argc,char**argv) {
//test01();
test02();
system("pause");
return 0;
}