网站首页 > 技术文章 正文
大家好,我是发哥,今天我们来说说C++中string类常用方法。
直接通过实例说明,大家遇到直接使用就好了。
示列代码如下:
#include <iostream>
#include <string>
using namespace std;
int main()
{
{
string val_str = "Hello World";
cout << "[cout] val_str is: " << val_str << endl;
// string 转换为 char*
const char* pchar_buf = val_str.c_str();
printf("[printf] pchar_buf is: %s\n", pchar_buf);
}
{
//比较两字符串是否相等
string str_one = "Hello World";
string str_two = "Hello World";
int nLen = str_one.length();
cout << "the length of str_one is: " << nLen << endl;
if (0 == str_one.compare(str_two))
{
cout << "str_one equal with str_two" << endl;
}
else
{
cout << "str_one not equal with str_two" << endl;
}
}
{
//判断是否是空字符串
string str;
if (str.empty())
{
cout << "str is empty." << endl;
}
else
{
cout << "str is not empty." << endl;
}
}
{
//char* 转换为 string
char *pchar_name = "lilei";
string strName;
strName = pchar_name;
cout << "strName is: " << strName << endl;
}
{
//字符串插入
string str = "I am";
str.insert(4, " fine.");
cout << "str is: " << str << endl;
}
return 0;
}
示列运行结果如下截图:
今天就说到这,谢谢你的关注,记得点赞、评论和关注哦,后期会有更好的优秀作品呈现,你的支持是我创建继续创造优秀作品的动力。
明天早上六点我们继续再聊!
猜你喜欢
- 2024-10-05 Python基础知识——字符串(python字符串的常用方法)
- 2024-10-05 你会在 Python 中使用字符串吗?(python字符串需要用什么包裹)
- 2024-10-05 javascript内置对象总结 - String
- 2024-10-05 Python 中字符串和字符串处理的综合指南
- 2024-10-05 Excel文本函数——CONCATENATE函数详解
- 2024-10-05 探究C语言中字符串大小和长度易错点
- 2024-10-05 【PythonTip题库300题】第47题:字符串中缺失的字母
- 2024-10-05 Rust: 字符串总结,虽常见但非常重要
- 2024-10-05 这些字符串处理函数你用过吗(字符串常用操作函数)
- 2024-10-05 C字符串搜索和替换算法(字符串查找替换c语言)
- 10-02基于深度学习的铸件缺陷检测_如何控制和检测铸件缺陷?有缺陷铸件如何处置?
- 10-02Linux Mint 22.1 Cinnamon Edition 搭建深度学习环境
- 10-02AWD-LSTM语言模型是如何实现的_lstm语言模型
- 10-02NVIDIA Jetson Nano 2GB 系列文章(53):TAO模型训练工具简介
- 10-02使用ONNX和Torchscript加快推理速度的测试
- 10-02tensorflow GPU环境安装踩坑日记_tensorflow配置gpu环境
- 10-02Keye-VL-1.5-8B 快手 Keye-VL— 腾讯云两卡 32GB GPU保姆级部署指南
- 10-02Gateway_gateways
- 最近发表
-
- 基于深度学习的铸件缺陷检测_如何控制和检测铸件缺陷?有缺陷铸件如何处置?
- Linux Mint 22.1 Cinnamon Edition 搭建深度学习环境
- AWD-LSTM语言模型是如何实现的_lstm语言模型
- NVIDIA Jetson Nano 2GB 系列文章(53):TAO模型训练工具简介
- 使用ONNX和Torchscript加快推理速度的测试
- tensorflow GPU环境安装踩坑日记_tensorflow配置gpu环境
- Keye-VL-1.5-8B 快手 Keye-VL— 腾讯云两卡 32GB GPU保姆级部署指南
- Gateway_gateways
- Coze开源本地部署教程_开源canopen
- 扣子开源本地部署教程 丨Coze智能体小白喂饭级指南
- 标签列表
-
- cmd/c (90)
- c++中::是什么意思 (84)
- 标签用于 (71)
- 主键只能有一个吗 (77)
- c#console.writeline不显示 (95)
- pythoncase语句 (88)
- es6includes (74)
- sqlset (76)
- apt-getinstall-y (100)
- node_modules怎么生成 (87)
- chromepost (71)
- flexdirection (73)
- c++int转char (80)
- mysqlany_value (79)
- static函数和普通函数 (84)
- el-date-picker开始日期早于结束日期 (76)
- js判断是否是json字符串 (75)
- c语言min函数头文件 (77)
- asynccallback (87)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- java (73)
- js数组插入 (83)
- mac安装java (72)
- 无效的列索引 (74)