网站首页 > 技术文章 正文
大家好,我是发哥,今天我们来说说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语言)
- 11-23申请恢复qq群(申请恢复qq群聊怎么恢复)
- 11-23苹果查询激活日期和保修期限
- 11-23u盘提示格式化但无法格式化(u盘提示格式化却无法格式化)
- 11-22pe启动盘怎么装系统(pe启动盘如何重装系统win10)
- 11-22软件商店一键安装(软件商店一键安装下载 - 百度下载拼多多)
- 11-22全球邮企业邮箱(全球邮企业邮箱app在哪下载)
- 11-22中国联想电脑官网(联想官方网站)
- 11-22浏览器官方下载安装(chrome浏览器官方下载)
- 最近发表
- 标签列表
-
- 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 (77)
- vector线程安全吗 (73)
- java (73)
- js数组插入 (83)
- mac安装java (72)
- 无效的列索引 (74)
