大家好:
衷心希望各位点赞。
您的问题请留在评论区,我会及时回答。
string基本概念
string是C++风格的字符串,本质上是一个类。
string 和 char* 的区别
char* 是一个指针;
string是一个类,内部封装了 char* ,用来管理字符串,是一个 char* 型的容器。
特点
string内部封装了很多成员函数
例如:查找find、拷贝copy、删除delete、替换replace、插入insert
string管理 char* 分配的内存,不用担心复制越界和取值越界等,由类内部负责。
string的构造函数
#include <iostream>
#include <Windows.h>
#include <string>
#include <vector>
#include <algorithm> // 使用STL提供的遍历算法,遍历vector容器
using namespace std;
void test()
{
// 默认构造
string s1 = "Hello World";
cout << "s1 = " << s1 << endl;
// 第二种构造函数
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(void)
{
test();
system("pause");
return 0;
}
运行截图:
string赋值操作
#include <iostream>
#include <Windows.h>
#include <string>
using namespace std;
void test()
{
// 第一种赋值方式
string s1 = "Hello World";
cout << "s1 = " << s1 << endl;
// 第二种赋值方式
string s2;
s2 = s1;
cout << "s2 = " << s2 << endl;
// 第三种赋值方式
string s3;
s3 = 'A';
cout << "s3 = " << s3 << endl;
// 第四种赋值方式
string s4;
s4.assign("Hello C++");
cout << "s4 = " << s4 << endl;
//第五种赋值方式
string s5;
s5.assign("Hello C++", 5);
cout << "s5 = " << s5 <<endl;
// 第六种赋值方式
string s6;
s6.assign(s5);
cout << "s6 = " << s6 <<endl;
// 第七种赋值方式
string s7;
s7.assign(10, 'W');
cout << "s7 = " << s7 << endl;
}
int main(void)
{
test();
system("pause");
return 0;
}
运行截图:
string字符串拼接
#include <iostream>
#include <Windows.h>
#include <string>
using namespace std;
void test()
{
// 第一种字符串拼接方式
string str1 = "Hello";
str1 += " C++";
cout << str1 << endl;
// 第二种字符串拼接方式
str1 += '!';
cout << str1 << endl;
// 第三种字符串拼接方式
string str3 = " Good morning";
str1 += str3;
cout << str1 << endl;
// 第四种字符串拼接方式
string str4 = "I";
str4.append(" Love ");
cout << str4 << endl;
// 第五种字符串拼接方式
string str5 = str4;
str5.append("C++ and Java", 3);
cout << str5 << endl;
// 第六种字符串拼接方式
string str6 = "你好:";
str6.append(str1);
cout << str6 << endl;
// 第七种字符串拼接方式
string str7;
str7.append(str1, 0, 9);
cout << str7 << endl;
}
int main(void)
{
test();
system("pause");
return 0;
}
运行截图:
string字符串查找和替换
#include <iostream>
#include <Windows.h>
#include <string>
using namespace std;
// 字符串查找
void test()
{
string s1 = "Hello World";
int pos = s1.find("Hello");
if (pos == -1)
{
cout << "未找到字符串" << endl;
}
else
{
cout << "pos = " << pos << endl; // pos = 0
}
}
// 字符串替换
void test02()
{
string str = "Hello World";
str.replace(6, 5, "C++");
cout << str << endl;
}
int main(void)
{
test();
test02();
system("pause");
return 0;
}
运行截图:
string字符串比较
#include <iostream>
#include <Windows.h>
#include <string>
using namespace std;
void test()
{
string s1 = "Hello World";
string s2 = "Hello";
if (s1.compare(s2) == 0)
{
cout << "字符串相等" << endl;
}
else
{
cout << "字符串不等" << endl;
}
}
int main(void)
{
test();
system("pause");
return 0;
}
运行截图:
string字符串存取
#include <iostream>
#include <Windows.h>
#include <string>
using namespace std;
void test()
{
string s = "Hello World";
// 1、通过[]访问单个字符
for (int i = 0; i < s.size(); i++)
{
cout << s[i] << " ";
}
cout << endl;
// 2、通过at访问单个字符
for (int i = 0; i < s.size(); i++)
{
cout << s.at(i) << " ";
}
cout << endl;
}
int main(void)
{
test();
system("pause");
return 0;
}
运行截图:
string子串获取
#include <iostream>
#include <Windows.h>
#include <string>
using namespace std;
void test()
{
string s1 = "Hello World";
string s2 = s1.substr(6, 5);
cout << s2 << endl; // 输出World
}
int main(void)
{
test();
system("pause");
return 0;
}
运行截图: