1string基本概念
本质:string是C++风格的字符串,而string本质上是一个类。
string和char*区别
char*是一个指针
string是一个类,类内部封装了char*,管理这个字符串,是一个char*型的容器。
特点:
string类内部封装了很多成员方法
列如:查找find,拷贝copy,删除delete,替换replace,插入insert
string管理char*所分配的内存,不用担心复制越界和取值越界等问题,由类内部进行负责
2.string构造函数
#include <iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
//构造函数原型
//string(); 创建一个空的字符串,列如string str;
//string(const char*s); 使字符串s初始化
//string(const string&str);//使用一个string对象初始化另一个string对象
//string(int a,char c);//使用a个字符c初始化
void test01() {
string s1;//默认构造
const char* str = "hello word";
string s2(str);
cout << "s2=" << s2;//结果s2=hello word
string s3(s2);//拷贝构造
cout << s3;
string s4(10, 'a');
cout << s4;//结果为aaaaaaaaaa
}
int main()
{
test01();
return 0;
}
3.string赋值操作
#include <iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
void test01() {
//第一种string&operator=(const*s); char*类型字符串 赋值给当前的字符串
string str1;
str1 = "hello world";
cout << "str1=" << str1;
//第二种
string str2;
str2 = str1;
//第三种
string str3;
str3 = 'a';
//第四种
string str4;
str4.assign("helloword");
//第五种 string&assign(const char*s,int n); 把字符串的前n个字符赋值给当前的字符串
string str5;
str5.assign("helloword", 5);
cout << str5;//输出结果为hello
//第六种
string str6;
str6.assign(str5);
//第七种
string str7;
str7.assign(10, 'w');
}
int main()
{
test01();
return 0;
}
4.string字符串拼接
功能:实现字符串末尾拼接字符串
#include <iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
void test01() {
string str1 = "我";
str1 += "爱玩游戏";
cout << "str1=" << str1;//str1=我爱玩游戏
str1 += ":";
cout << str1;//我爱玩游戏:
string str2 = "LOL DNF";
str1 += str2;//
cout << str2;//我爱玩游戏:LOL DNF
string str3 = "I";
str3.append(" love ");
cout << str3;//I love
str3.append("game abdcdd", 4);//把game abdcdd的前四个字母拼接给str3
cout << str3;//I love game
//str3.append(str2);
//cout << str3; //I love gameLOL DNF
str3.append(str2, 0, 3);//截取LOL ;从4,6为DNF
cout << str3;//I love gameLOL .
}
int main()
{
test01();
return 0;
}
5.string查找与替换
查找:查找指定字符串是否存在
替换:在指定位置替换字符串
#include <iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
//1.查找
void test01() {
//原型 int find(const string&str,int pos=0)const; //查找str第一次出现的位置,从pos开始查找
string str1 = "abcdefg";
int pos=str1.find("de");
cout << pos;//pos=3,从零开始索引,第三个为de字符串
int pos1 = str1.find("df");
cout << pos1;//pos=-1.未找到字符串
//rfind
pos = str1.rfind("de");
cout << pos;//pos=3,rfind从右往左查找,find从左往右查找
}
//2.替换
void test02() {
string str2 = "abcdefg";
str2.replace(1, 3, "1111");
cout << str2;//str="a1111efg" 把bcd替换成1111了
}
int main()
{
test01();
test02();
return 0;
}
find找到字符串后返回查找的第一个字符位置,找不到返回-1
6.string字符串比较
比较方式:字符串比较是按字符的ASCII码进行对比
=返回0
>返回1
<返回-1
#include <iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
//字符串比较 对比方式:一个一个比 汉字之前比较一般没有意义
//compare最大用途是比较两个字符串是否相等
void test01() {
string str1 = "xello";
string str2 = "hello";
if (str1.compare(str2) == 0) {
cout << "str1 等于 str2";
}
else if(str1.compare(str2) > 0) {
cout << "str1>str2";
}
}
int main()
{
test01();
return 0;
}
7.字符存取
#include <iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
//string 字符存取
void test01() {
string str1 = "hello";
//1.通过[]访问单个字符
for (int i = 0; i < str1.size(); i++) { //str1.size()来返回字符串的长度
cout << str1[i]<<" ";
}
//2.通过at方式来访问单个字符
for (int i = 0; i < str1.size(); i++) {
cout << str1.at(i) << " ";
}
//修改单个字符
str1[0] = 'x';
cout << str1;//xello
str1.at(1) = 'x';
cout << str1 << endl;//xxllo
}
int main()
{
test01();
return 0;
}
8.string插入和删除
#include <iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
//字符串的插入和删除
void test01() {
string str = "hello";
//插入
str.insert(1, "111");
cout << str;//h111ello "111"这里也可以放一个字符串str1
//删除
str.erase(1, 3);//从第一个位置起删除三个
cout << str;//hello
}
int main()
{
test01();
return 0;
}
注:插入和删除都是从0开始的。
9.string字符串
功能:从字符串中获取想要的子串
#include <iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
//string求子串
void test01() {
string str = "abcdef";
string subStr = str.substr(1, 3);//从第一个截到第三个字符
cout << subStr;//bcd
}
void test02() {
string email = "zhangsan@sina.com";
//从邮件地址中获取用户信息
int pos = email.find("@");//8
string usename = email.substr(0, pos);
cout << usename;
}
int main()
{
test01();
test02();
return 0;
}
461

被折叠的 条评论
为什么被折叠?



