一、string
基本概念
- 本质:string 是C++风格的字符串,而 string 本质上是一个类。
string
和char*
区别:- char* 是一个指针
- string 是一个类,类内部封装了char* ,管理这个字符串,是一个 char* 型的容器。
- 特点:
- string 类内部封装了很多成员方法。
- 例如:查找find、拷贝copy、删除delete、插入insert
- string管理char* 所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责。
二、string
构造函数
构造函数原型:
string(); //创建一个空的字符串,例如:string str;
string(const char* s); //使用字符串初始化
string(const string& str); //使用一个string对象初始化另一个string对象
string(int n, charc); //使用n个字符c初始化
代码测试:
#include<iostream>
using namespace std;
#include<string>
//STL中的每个容器在使用时都需要包含头文件
#include<vector>
#include<algorithm> //标准算法头文件
//构造字符串
void test01()
{
string s1; //默认构造
//string(const char* s);
const char* str = "hello world";
string s2(str);
cout << "s2 = " << s2 << endl;
//string(const string& str);
string s3(s2);
cout << "s3 = " << s3 << endl;
//string(int n, charc);
string s4(10, 'a');
cout << "s4 = " << s4 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
输出:
s2 = hello world
s3 = hello world
s4 = aaaaaaaaaa
请按任意键继续. . .
三、string
赋值操作
功能:给string字符串进行赋值
赋值的函数模型:
//char* 类型字符串 赋值给当前的字符串
string& operatot=(const char* s);
//把字符串s赋给当前的字符串
string& operator=(const string &s);
//字符赋给当前的字符串
string& operator=(char c)
//把字符串s赋给当前的字符串
string& assign(const char* s);
//把字符串s的前n个字符赋给当前的字符串
string& assign(const string &s,int n);
//把字符串s赋给当前字符串
string& assign(sonst string &s);
//用n个字符c赋给当前字符串
string& assign(int n,char c);
代码测试:
#include<iostream>
using namespace std;
#include<string>
//STL中的每个容器在使用时都需要包含头文件
#include<vector>
#include<algorithm> //标准算法头文件
//字符串赋值
void test01()
{
//char* 类型字符串 赋值给当前的字符串
string str1;
str1 = "hello world";
cout << "str1 = " << str1 << endl;
//把字符串s赋给当前的字符串
string str2;
str2 = str1;
cout << "str2 = " << str2 << endl;
//字符赋给当前的字符串
string str3;
str3 = 'a';
cout << "str3 = " << str3 << endl;
//assign -- char* 类型字符串 赋值给当前的字符串
string str4;
str4.assign("hello C++");
cout << "str4 = " << str4 << endl;
//把字符串s的前n个字符赋给当前的字符串
string str5;
str5.assign("hello world", 5);
cout << "str5 = " << str5 << endl;
//assign -- 把字符串s赋给当前字符串
string str6;
str6.assign(str5);
cout << "str6 = " << str6 << endl;
//assign -- 用n个字符c赋给当前字符串
string str7;
str7.assign(10,'a');
cout << "str7 = " << str7 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
输出:
str1 = hello world
str2 = hello world
str3 = a
str4 = hello C++
str5 = hello
str6 = hello
str7 = aaaaaaaaaa
请按任意键继续. . .
四、string
字符串拼接
功能描述:在字符串末尾拼接字符串
函数原型:
代码测试:
#include<iostream>
using namespace std;
#include<string>
//STL中的每个容器在使用时都需要包含头文件
#include<vector>
#include<algorithm> //标准算法头文件
//字符串拼接 -- 会改变原字符串
void test01()
{
string str1 = "我";
str1 += "爱玩游戏";
cout << "str1 = " << str1 << endl;
str1 += ":";
cout << "str1 = " << str1 << endl;
string str2 = " 和平精英 王者荣耀";
str1 += str2;
cout << "str1 = " << str1 << endl;
string str3 = "I";
str3.append(" love ");
cout << "str3 = " << str3 << endl;
str3.append("game LOL", 4);
cout << "str3 = " << str3 << endl;
//str3.append(str2);
//从第0个开始,截取9个字符,一个汉字两个字符
//str3.append(str2, 0, 9); //输出:str3 = I love game 和平精英
//从编号为9开始,截取9个字符
str3.append(str2, 9, 9); //输出:str3 = I love game 王者荣耀
cout << "str3 = " << str3 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
输出:
str1 = 我爱玩游戏
str1 = 我爱玩游戏:
str1 = 我爱玩游戏: 和平精英 王者荣耀
str3 = I love
str3 = I love game
str3 = I love game 王者荣耀
请按任意键继续. . .
五、string
查找和替换
功能描述:
- 查找指定字符串是否存在
- 在指定的位置替换字符串
函数原型:
代码测试:
#include<iostream>
using namespace std;
#include<string>
//STL中的每个容器在使用时都需要包含头文件
#include<vector>
#include<algorithm> //标准算法头文件
//字符串查找
void test01()
{
//find
string str1 = "abcdefgde";
int pos = str1.find("de");
int pos2 = str1.find("ab");
cout << "pos = " << pos << endl;//输出:pos = 3,注意编号从0开始
//cout << "pos2 = " << pos2 << endl; //没有,输出:pos2 = -1
if (pos2 == -1)
{
cout << "未找到字符串" << endl;
}
else
{
cout << "pos2 = " << pos2 << endl;
}
//rfind 和 find 区别
//rfdind从右往左查找 find 从左往右查找
int pos3 = str1.rfind("de");
cout << "pos3 = " << pos3 << endl; //输出:pos3 = 7
}
//字符串与替换
void test02()
{
string str1 = "abcdefg";
//从编号为1的字符起,替换3个字符,即替换 bcd 为 1111
str1.replace(1, 3, "1111"); //四个 1 都会被替换进去
cout << "str1 = " << str1 << endl; //输出:str1 = a1111efg
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
输出:
pos = 3
pos2 = 0
pos3 = 7
str1 = a1111efg
请按任意键继续. . .
总结:
六、string
字符串比较
比较方式:
- 按照字符的ASCII码进行逐个对比,三种结果:
- 返回 0 两个字符串相等
- 返回 1 第一个字符串大于第二个字符串
- 返回 -1 第一个字符串小于第二个字符串
- 函数原型:
int compare(const string &s) const; //与字符串s比较
int compare(const char* s) const; //与字符串s比较
代码测试:
#include<iostream>
using namespace std;
#include<string>
//STL中的每个容器在使用时都需要包含头文件
#include<vector>
#include<algorithm> //标准算法头文件
//字符串比较
void test01()
{
string str1 = "mello";
string str2 = "hello1";
if (str1.compare(str2) == 0)
{
cout << "str1 = str2" << endl;
}
else if(str1.compare(str2) == 1)
{
cout << "str1 > str2" << endl;
}
else if (str1.compare(str2) == -1)
{
cout << "str1 < str2" << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
输出:
str1 > str2
请按任意键继续. . .
七、string
字符存取
string中单个字符存取方式有两种:
char& operator[](int n); //通过[]方式取字符
char& at(int n); //通过at方式获取字符
代码测试:
#include<iostream>
using namespace std;
#include<string>
//STL中的每个容器在使用时都需要包含头文件
#include<vector>
#include<algorithm> //标准算法头文件
//字符串字符存取
void test01()
{
string str = "hello";
//cout << "str = " << str << endl;
//读取
//1、通过[]方式访问单个字符
for (int i = 0; i < str.size(); i++)
{
cout << str[i] << " ";
}
cout << endl;
//2、通过at方式访问单个字符
for (int i = 0; i < str.size(); i++)
{
cout << str.at(i) << " ";
}
cout << endl;
//修改单个字符
str[0] = 'x';
cout << "str = " << str << endl;
str.at(1) = 'x';
cout << "str = " << str << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
输出:
h e l l o
h e l l o
str = xello
str = xxllo
请按任意键继续. . .
八、string
插入和删除
函数原型:
代码测试:
#include<iostream>
using namespace std;
#include<string>
//STL中的每个容器在使用时都需要包含头文件
#include<vector>
#include<algorithm> //标准算法头文件
//字符串字符插入与删除
void test01()
{
string str = "hello";
//插入,从编号为1位置开始插入3个字符111
str.insert(1, "111");
cout << "str = " << str << endl; //输出:str = h111ello
//删除,从编号为1位置开始删除3个字符
str.erase(1, 3);
cout << "str = " << str << endl; //输出:str = hello
}
int main()
{
test01();
system("pause");
return 0;
}
输出:
str = h111ello
str = hello
请按任意键继续. . .
九、string
子串获取
函数原型:
//返回由pos开始的n个字符组成的字符串
string substr(int pos = 0,int n = npos) const;
代码测试:
#include<iostream>
using namespace std;
#include<string>
//STL中的每个容器在使用时都需要包含头文件
#include<vector>
#include<algorithm> //标准算法头文件
//字符串字截取
void test01()
{
string str = "abcdef";
string subStr = str.substr(1, 3);
cout << "subStr = " << subStr << endl; //输出:subStr = bcd
}
void test02()
{
string email = "zhangsan@sina.com";
//从邮件地址中 获取 用户名信息 zhangsan
int pos = email.find('@');
string userName = email.substr(0, pos);
cout << "userName = " << userName << endl;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
输出:
subStr = bcd
userName = zhangsan
请按任意键继续. . .