C++:string 类

在C++中定义一个 std::string 字符串可以采用以下几种方式:

1.使用字符串字面量初始化:
cpp 复制代码
std::string str = "Hello, world!";
2.使用构造函数初始化:
cpp 复制代码
std::string szStringB("Hello wolven");
3.使用重复字符初始化:
cpp 复制代码
std::string szStringC(6, 'w');

无论采用哪种方式,都可以创建一个 std::string 类型的字符串。选择哪种方式取决于代码的需求和个人偏好。

string处理字符串的方法

std::string 类定义在 <string> 头文件中,它提供了一系列方法来操作字符串,例如计算长度、查找、插入、删除、比较等。

长度计算:length()
cpp 复制代码
std::string szStringA = "Hello,world";
std::cout << "Length:" << szStringA.length() << std::endl;
字符串查找 find()
cpp 复制代码
std::string szStringA = "Hello,world C++";
​
size_t pos = szStringA.find("C++");
​
if (pos != std::string::npos) {
    std::cout << "Found String Positon at:" << pos << std::endl;
}else{
    std::cout << "Not Found String" << std::endl;
}

使用 find 方法查找子字符串 "C++" 在 szStringA 中的位置,并将结果保存在变量 pos 中。如果 find 方法找到了子字符串,则返回子字符串在原字符串中的起始位置;如果没有找到,则返回 std::string::npos,表示未找到。最后,根据 pos 的值,输出相应的消息,指示是否找到了字符串。

字符串插入方法 insert()

1.插入单个字符: 使用 insert 方法可以在指定位置插入单个字符

cpp 复制代码
std::string szStringA = "Hello,world C++";
szStringA.insert(5,1,'0');
std::cout << szStringA << std::endl;

在szStringA字符串的位置5插入一个'0'

cpp 复制代码
输出结果:Hell0,world C++

2.插入字符串: 使用 insert 方法可以在指定位置插入另一个字符串。

cpp 复制代码
std::string szStringA = "Hello,world C++";
std::string szStringB(" Hello wolven");
szStringA.insert(15, szStringB);
std::cout << szStringA << std::endl;

在szStringA 字符串的位置15插入字符串 " Hello wolven"

cpp 复制代码
输出结果:Hello,world C++ Hello wolven

3.插入部分子字符串: 使用 insert 方法可以在指定位置插入另一个字符串的一部分。

cpp 复制代码
std::string szStringA = "Hello,world C++";
std::string szStringB(" Hello wolven");
szStringA.insert(15, szStringB,0,6);
std::cout << szStringA << std::endl;

在位置15插入字符串 szStringB的子字符串位置0到6的位置也就是" Hello"

cpp 复制代码
输出结果:Hello,world C++ Hello
字符串删除方法 erase()

**1.删除单个字符:**删除指定位置的单个字符。

cpp 复制代码
std::string szStringA = "Hello,world C++";
szStringA.erase(5,1);
std::cout << szStringA << std::endl;

删除szStringA字符串位置5的1个字符,即逗号

cpp 复制代码
输出结果:Helloworld C++

**2.删除子字符串:**删除指定范围内的子字符串。

cpp 复制代码
std::string szStringA = "Hello,world C++";
szStringA.erase(5,6);
std::cout << szStringA << std::endl;

删除szStringA字符串位置5开始算起的6个字符,即",world"

cpp 复制代码
输出结果:Hello C++

**3.删除到字符串末尾:**删除从指定位置到字符串末尾的所有字符。

cpp 复制代码
std::string szStringA = "Hello,world C++";
szStringA.erase(5);

从位置5开始删除到字符串末尾的所有字符,即 ", world C++"

cpp 复制代码
输出结果:Hello
字符串的比较方法 compare()

compare 方法返回一个整数,表示两个字符串的大小关系。如果字符串相等,则返回0;如果第一个字符串小于第二个字符串,则返回负数;如果第一个字符串大于第二个字符串,则返回正数。

cpp 复制代码
std::string szStringA = "Hello,world C++";
std::string szStringB(" Hello wolven");

int res = szStringA.compare(szStringB);
if (res > 0) {
	std::cout << "szStringA > szStringB" << std::endl;
}
else if(res < 0){
	std::cout << "szStringA > szStringB" << std::endl;
}
else
{
	std::cout << "szStringA  =  szStringB" << std::endl;
}

输出结果:

在C++中,字符串比较大小通常是按照字典序进行的,也就是按照字符的ASCII值进行比较。这意味着,如果两个字符串的第一个字符不同,那么这两个字符串的大小比较就取决于它们第一个不同的字符的ASCII值。

字符串替换方法replace()

这个方法可以用于替换指定位置和长度的子字符串为另一个字符串。

cpp 复制代码
std::string szStringA = "Hello,world C++";
std::string szStringB("wolven");

size_t pos = szStringA.find("C++");
if (pos != std::string::npos) {
	szStringA.replace(pos, 3, szStringB);
}

std::cout << szStringA << std::endl;

使用 find 方法查找字符串 szStringA 中子字符串 "C++" 的位置,并将结果保存在变量 pos 中。如果找到了 "C++",则执行以下操作:使用 replace 方法将字符串 szStringA 中从位置 pos 开始的 3 个字符(即 "C++")替换为字符串 szStringB("wolven")。最后,代码输出替换后的 szStringA

字符串拼接: + 加号操作符
cpp 复制代码
std::string szStringA = "Hello,world C++";
std::string szStringB("wolven");

std::string szStringResult = szStringA + szStringB;
std::cout << szStringResult << std::endl;

加号运算符可以将两个字符串连接起来,并产生一个新的字符串。

在 C++ 中,+ 操作符不仅可以用于数值的加法,还可以用于字符串的拼接。这是因为 C++ 对于字符串类型 std::string 重载了 + 操作符,使其能够执行字符串的连接操作。

下一篇文章就来说一下运算符重载。
相关推荐
柏箱1 分钟前
PHP基本语法总结
开发语言·前端·html·php
学无止境\n7 分钟前
[C语言]指针和数组
c语言·数据结构·算法
进阶的架构师11 分钟前
互联网Java工程师面试题及答案整理(2024年最新版)
java·开发语言
黄俊懿11 分钟前
【深入理解SpringCloud微服务】手写实现各种限流算法——固定时间窗、滑动时间窗、令牌桶算法、漏桶算法
java·后端·算法·spring cloud·微服务·架构
新缸中之脑11 分钟前
Llama 3.2 安卓手机安装教程
前端·人工智能·算法
易辰君13 分钟前
python爬虫 - 深入requests模块
开发语言·爬虫·python
木子020420 分钟前
java高并发场景RabbitMQ的使用
java·开发语言
无夜_20 分钟前
Prototype(原型模式)
开发语言·c++
看到请催我学习21 分钟前
内存缓存和硬盘缓存
开发语言·前端·javascript·vue.js·缓存·ecmascript
夜雨翦春韭31 分钟前
【代码随想录Day29】贪心算法Part03
java·数据结构·算法·leetcode·贪心算法