string操作指南
#include <iostream>
using namespace std;
#include <string>
//一、构造函数
/*
string(); 创建一个空的字符串,例如string str;
string(const char* s); 使用字符串s初始化
string(const string& str); 使用一个string对象初始化另一个string对象
string(int n, char c); 使用n个字符c初始化
*/
void test001()
{
string s1;
const char* str = "hello";
string s2(str);
cout << "s2=" << s2 << endl;
string s3(s2);
cout << "s3=" << s3 << endl;
string s4(5, 'a');
cout << "s4=" << s4 << endl;
}
//二、赋值操作
/*
string& operator=(const char* s); char*类型字符串 赋值给当前的字符串
string& operator=(const string &s); 把字符串s赋值给当前的字符串
string& operator=(char c); 字符赋值给当前的字符串
string& assign(const char *s); 把字符串s赋值给当前的字符串
string& assign(const char *s, int n); 把字符串s的前n个字符赋值给当前的字符串
string& assign(const string &s); 把字符串s赋值给当前字符串
string& assign(int n, char c); 把n个字符c赋值给当前字符串
*/
void test002()
{
string str1;
str1 = "hello";
cout << "str1:" << str1 << endl;
string str2 = str1;
cout << "str2:" << str2 << endl;
string str3;
str3 = 'a';
cout << "str3:" << str3 << endl;
string str4;
str4.assign("hello");
cout << "str4:" << str4 << endl;
string str5;
str5.assign("hello", 2);
cout << "str5:" << str5 << endl;
string str6;
str6.assign(str5);
cout << "str6:" << str6 << endl;
string str7;
str7.assign(2, 'h');
cout << "str7:" << str7 << endl;
}
//三、字符串拼接:实现在字符串末尾拼接字符串
/*
string& operator+=(const char* str); 重载+=操作符
string& operator+=(const char c); 重载+=操作符
string& operator+=(const string& str); 重载+=操作符
string& append(const char *s); 把字符串s连接到当前字符串结尾
string& append(const char *s, int n); 把字符串s的前几个字符连接到当前字符串结尾
string& append(const string &s); 同operator+=(const string &str)
string& append(const string &s, int pos, int n); 字符串s中从pos开始的n个字符连接到字符串结尾
*/
void test003()
{
string str1 = "我";
str1 += "爱玩游戏";
cout << "str1=" << str1 << endl;
str1 += '!';
cout << "str1=" << str1 << endl;
string str2 = "lol";
str1 += str2;
cout << "str1=" << str1 << endl;
string str3 = "I";
str3.append(" love ");
cout << "str3=" << str3 << endl;
str3.append(" love ", 6);
cout << "str3=" << str3 << endl;
string str4 = "l ";
str3.append(str4);
cout << "str3=" << str3 << endl;
str3.append(str4, 0, 1);
cout << "str3=" << str3 << endl;
}
//四、字符串查找和替换
/*
查找:查找指定字符串是否存在
替换:在指定的位置替换字符串
int find(const string& str, int pos = 0) const; 查找str第一次出现位置,从pos开始查找
int find(const char* s, int pos = 0) const; 查找s第一次出现位置,从pos开始查找
int find(const char* s, int pos, int n) const; 从pos位置查找s的前n个字符的第一次位置
int find(const char c, int pos = 0) const; 查找字符c第一次出现的位置
int rfind(const string& str, int pos = npos) const; 查找str最后一次位置,从pos开始查找
int rfind(const char* s, int pos = npos) const; 查找s最后一次出现位置,从pos开始查找
int rfind(const char*s, int pos, int n) const; 从pos查找s的前n个字符最后一次出现的位置
int rfind(const char c, int pos = 0) const; 查找字符c最后一次出现的位置
string& replace(int pos, int n, const string& str); 替换从pos开始n个字符为字符串str
string& replace(int pos, int n, const char* s); 替换从pos开始的n个字符为字符串s
*/
void test004()
{
string str1 = "abc1defgde";
int pos = str1.find("de"); //如果有就是返回位置,没有就返回-1
cout << pos << endl;
int pos2 = str1.rfind("de");
cout << pos2 << endl;
cout << str1.replace(0, 3, "lll") << endl;
}
//五、字符串比较
/*
比较方式:字符串比较是按字符的ASCII码进行,主要用于表示两个字符串是否相等
=返回0, >返回1, < 返回-1
int compare(const string & s) const; 与字符串s比较
int compare(const char *s) const; 与字符串s比较
*/
void test005()
{
string str1 = "hello";
string str2 = "hello";
if (str1.compare(str2) == 0)
{
cout << "str1=str2" << endl;
}
else if (str1.compare(str2) > 0)
{
cout << "str1>str2" << endl;
}
else if (str1.compare(str2) < 0)
{
cout << "str1<str2" << endl;
}
}
//六、字符存取
/*
string中单个字符存取方式有两种
char& operator[](int n); 通过[]方式取字符
char& at(int n); 通过at方式获取字符
*/
void test006()
{
string str1 = "hello";
cout << "str1=" << str1 << endl;
for (int i = 0; i < str1.size(); i++)
{
cout << str1[i] << endl;
}
for (int i = 0; i < str1.size(); i++)
{
cout << str1.at(i) << endl;
}
str1[0] = 'x';
str1.at(1) = 'x';
for (int i = 0; i < str1.size(); i++)
{
cout << str1.at(i) << endl;
}
}
//七、字符串插入和删除
/*
对string字符串进行插入和删除字符操作
string& insert(int pos, const char* s); 插入字符串
string& insert(int pos, const string& str); 插入字符串
string& insert(int pos, int n, char c); 在指定位置插入n个字符c
string& erase(int pos, int n = npos); 删除从pos位置开始的n个字符
*/
void test007()
{
string str1 = "hello";
str1.insert(0, "111");
cout << "str1:" << str1 << endl;
str1.erase(0, 3);
cout << "str1:" << str1 << endl;
}
//八、子串获取
/*
从字符串中获取想要的子串
string substr(int pos = 0, int n = npos) const; 返回由pos开始的n个字符组成的字符串
*/
void test008()
{
string str1 = "abcdefg";
string substr = str1.substr(1, 3);
cout << "substr:" << substr << endl;
}
int main_string()
{
test008();
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下