当前位置: 首页 > news >正文

string容器

目录

string函数的构造

string赋值操作

string字符串拼接

string字符串查找和替换

string字符串比较

string字符存取

string插入与删除

string字串


string函数的构造

#include<iostream>
#include<cstring>
using namespace std;
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()
{test01();system("pause");return 0;
}

string赋值操作

//author:至尊宝
//time:2024.5.5
#include<iostream>
#include<cstring>
using namespace std;
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, 'b');cout << "str7 = " << str7 << endl;
}
int main()
{test01();system("pause");return 0;
}

string字符串拼接

//author:至尊宝
//time:2024.5.5
#include<iostream>
#include<cstring>
using namespace std;
void test01()
{string str1 = "我";str1 += "爱玩游戏";cout << "str1 = " << str1 << endl;str1 += ':';cout << "str1 = " << str1 << endl;string str2 = "LOL DNF";str1 += str2;cout << "str1 = " << str1 << endl;string str3 = "I";str3.append(" love ");cout << "str3 = " << str3 << endl;str3.append("game abcde", 4);cout << "str3 = " << str3 << endl;/*str3.append(str2);cout << "str3 = " << str3 << endl;*///截取DNFstr3 += ' ';str3.append(str2, 4, 3);cout << "str3 = " << str3 << endl;
}
int main()
{test01();system("pause");return 0;
}

string字符串查找和替换

//author:至尊宝
//time:2024.5.5
#include<iostream>
#include<cstring>
using namespace std;
void test01()
{string str1 = "abcdefg";int pos = str1.find("deo");if (pos == -1){cout << "未找到字符串" << endl;}else{cout << "找到字符串" << ",pos = " << pos << endl;}//rfindpos = str1.rfind("de");cout << "pos = " << pos << endl;//rfind从右往左查找,find从左往右查找
}
void test02()
{string str1 = "abcdefg";//从一号位置起3个字符替换为1111str1.replace(1, 3, "1111");cout << "str1 = " << str1 << endl;
}
int main()
{test02();system("pause");return 0;
}

string字符串比较

//author:至尊宝
//time:2024.5.5
#include<iostream>
#include<cstring>
using namespace std;
void test01()
{string str1 = "xello";string str2 = "hello";if (str1.compare(str2) == 0){cout << "str1等于str2" << endl;}else if (str1.compare(str2) > 0){cout << "str1大于str2" << endl;}else{cout << "str1小于str2" << endl;}
}int main()
{test01();return 0;
}

string字符存取

//author:至尊宝
//time:2024.5.5
#include<iostream>
#include<cstring>
using namespace std;
void test01()
{string str = "hello";cout << "str = " << str << endl;//单个字符//1.中括号for (int i = 0; i < str.size(); i++){cout << str[i];}cout << endl;//2.atfor (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();return 0;
}

string插入与删除

//author:至尊宝
//time:2024.5.5
#include<iostream>
#include<cstring>
using namespace std;
void test01()
{string str = "hello";//插入str.insert(1, "111");cout << "str = " << str << endl;//删除str.erase(1, 3);cout << "str = " << str << endl;
}int main()
{test01();return 0;
}

string字串

//author:至尊宝
//time:2024.5.5
#include<iostream>
#include<cstring>
using namespace std;
void test01()
{string str = "abcdef";string str2 = str.substr(1, 3);cout << str2 << endl;
}
void test02()
{string email = "hello@sina.com";//从邮件地址中获取用户名信息int pos = email.find("@");string name = email.substr(0, pos);cout << name << endl;
}int main()
{test02();return 0;
}

http://www.lryc.cn/news/344248.html

相关文章:

  • Ansible-inventory和playbook
  • HI3516CV610
  • ansible内置主机变量及魔法变量
  • 设计模式一
  • MySQL中JOIN连接的实现算法
  • [力扣题解] 216. 组合总和 III
  • Spring Security Oauth2 JWT 添加额外信息
  • 蜜蜂收卡系统 加油卡充值卡礼品卡自定义回收系统源码 前后端开源uniapp可打包app
  • 三星硬盘好还是西数硬盘好?硬盘数据丢失怎么找回
  • 企业微信hook接口协议,ipad协议http,设置是否自动同意
  • 自动化测试的成本高效果差,那么自动化测试的意义在哪呢?
  • h5页面用js判断机型是安卓还是ios,判断有app安装没app跳转应用商店app stroe或者安卓应用商店
  • 算法人生(17):从“课程学习”到“逐步暴露心理疗法”
  • C++仿函数周边及包装器
  • 改进灰狼算法优化随机森林回归预测
  • Hadoop生态系统的核心组件探索
  • 命令行方式将mysql数据库迁移到达梦数据库(全步骤)
  • 旅游系列之:庐山美景
  • 杭州恒生面试,社招,3年经验
  • python virtualenv 创建虚拟环境指定python版本,pip 从指定地址下载某个包
  • open feign支持调用form-data的接口
  • ESD静电问题 | TypeC接口整改
  • 基于springboot+mybatis+vue的项目实战之前端
  • 开源软件托管平台gogs操作注意事项
  • Linux cmake 初窥【3】
  • centos学习- ps命令详解-进程监控的利器
  • C++贪心算法
  • 访问网络附加存储:nfs
  • jsp 实验12 servlet
  • 「 网络安全常用术语解读 」通用配置枚举CCE详解