C++之string

string 是C++中常见的一个用于处理和操作字符串的类。一直有用它来存储字符串,今天来介绍介绍一下它的定义和一些基本用法吧。

1、头文件

cpp 复制代码
#include <string>

2、定义和初始化

cpp 复制代码
#include <iostream>
#include <string>
using namespace std;

int main() {
    // 默认初始化,创建一个空字符串
    string s1; 

    // 使用字符串字面量初始化
    string s2 = "Hello"; 

    // 使用另一个字符串初始化
    string s3(s2); 

    // 使用重复字符初始化
    string s4(5, 'a'); 

    cout << "s1: " << s1 << endl;
    cout << "s2: " << s2 << endl;
    cout << "s3: " << s3 << endl;
    cout << "s4: " << s4 << endl;

    return 0;
}

3、基本操作

1) 字符串拼接

可以使用**+** 或 += 操作符进行字符串拼接

cpp 复制代码
#include <iostream>
#include <string>
using namespace std;

int main() {
    string s1 = "Hello";
    string s2 = " World";

    // 使用 + 操作符拼接
    string s3 = s1 + s2;

    // 使用 += 操作符追加
    s1 += s2;

    cout << "s3: " << s3 << endl;
    cout << "s1: " << s1 << endl;

    return 0;
}    
2)字符串长度

可以使用 size() 或者**length()**方法获取字符串的长度

cpp 复制代码
#include <iostream>
#include <string>
using namespace std;

int main() {
    string s = "Hello";
    cout << "Length of s: " << s.size() << endl;
    cout << "Length of s: " << s.length() << endl;
    return 0;
}    
3)访问字符

可以通过**[ ]**操作符或者 **at( )**方法访问字符串中的字符

cpp 复制代码
#include <iostream>
#include <string>
using namespace std;

int main() {
    string s = "Hello";
    cout << "First character: " << s[0] << endl;
    cout << "Second character: " << s.at(1) << endl;

    //试一下越界访问
    cout << "use []: " << s[6] << endl;
    cout << "use at(): " << s.at(6) << endl;
    return 0;
}    

\] 操作符不进行边界检查,越界访问返回空,而 at() 方法会进行边界检查,若越界会抛出 std::out_of_range 异常。 ##### 3.4 字符串比较 可以使用 **==** 、**!=** 、**\<** 、**\>** 等操作符对字符串进行比较(哇咔咔这真的比C语言方便太多了) ```cpp #include #include using namespace std; int main() { string s1 = "Hello"; string s2 = "World"; if (s1 == s2) { cout << "s1 and s2 are equal." << endl; } else { cout << "s1 and s2 are not equal." << endl; } return 0; } ``` ![](https://i-blog.csdnimg.cn/direct/27ffae31db38498cb77022053b1abfb5.png) #### 4、常用成员函数 ##### 1)substr() 提取子字符串 ```cpp #include #include using namespace std; int main() { string s = "Hello World"; string sub = s.substr(6, 5); // 从索引 6 开始,提取长度为 5 的子字符串 cout << "result is: " << sub << endl; return 0; } ``` ![](https://i-blog.csdnimg.cn/direct/6ddcc4d991a642dab6773b5a759ae434.png) ##### 2)find() 查找子字符串或字符在字符串中的位置 ```cpp #include #include using namespace std; int main() { string s = "Hello World"; size_t pos = s.find("World"); if (pos != string::npos) { cout << "Found at position: " << pos << endl; } else { cout << "Not found." << endl; } return 0; } ``` ![](https://i-blog.csdnimg.cn/direct/c67833ad78674e5b8ad21c3d583f8a0f.png) ##### 3)erase() 删除字符串中的某一部分 ```cpp #include #include using namespace std; int main() { string s = "Hello World"; s.erase(5, 6); // 从索引 5 开始,删除长度为 6 的子字符串 cout << "After erase: " << s << endl; return 0; } ``` ![](https://i-blog.csdnimg.cn/direct/c6b31f2ade2e451fb94d719d9128e866.png) ##### 4)insert() 在字符串指定位置插入字符或子字符串 ```cpp #include #include using namespace std; int main() { string str = "Hello World"; // 在索引 5 处插入 ", " str.insert(5, ", "); cout << str << endl; return 0; } ``` ![](https://i-blog.csdnimg.cn/direct/66bef171a31e4d929a4eb4496ef76c29.png) ##### 5)replace() 替换字符串中指定位置的字符或子字符串 ```cpp #include #include using namespace std; int main() { string str = "Hello World"; // 从索引 6 开始,替换长度为 5 的子字符串为 "C++" str.replace(6, 5, "C++"); cout << str << endl; return 0; } ``` ![](https://i-blog.csdnimg.cn/direct/fad17b27e9b548e5a8497ed66c5c6744.png) ##### 6)reverse() 反转字符串中的字符顺序。在 \ 头文件中,需结合 std:: 使用 ```cpp #include #include #include using namespace std; int main() { string str = "Hello"; reverse(str.begin(), str.end()); cout << str << endl; return 0; } ``` ![](https://i-blog.csdnimg.cn/direct/a480c41ebec34eebbea7b69091de51f1.png) ##### 7)clear() 清空字符串中的所有字符,使其长度变为 0 ```cpp #include #include using namespace std; int main() { string str = "Hello"; str.clear(); cout << "Length after clear: " << str.length() << endl; return 0; } ``` ![](https://i-blog.csdnimg.cn/direct/f219b1e723944e5889663c06925b94b0.png) ##### **8)empty()** 判断字符串是否为空,若为空则返回 true,否则返回 false ```cpp #include #include using namespace std; int main() { string str1 = ""; string str2 = "Hello"; cout << "str1 is empty: " << (str1.empty() ? "Yes" : "No") << endl; cout << "str2 is empty: " << (str2.empty() ? "Yes" : "No") << endl; return 0; } ``` ![](https://i-blog.csdnimg.cn/direct/e4030c004a7f485ca7fe1e235c246d8e.png) #### 5、与 C 风格字符串的转换 ##### 1)string 转 C 风格字符串 可以使用 c_str() 方法获取指向以 '\\0' 结尾的字符数组的指针 ```cpp #include #include using namespace std; int main() { string s = "Hello"; const char* cstr = s.c_str(); cout << "C-style string: " << cstr << endl; return 0; } ``` ![](https://i-blog.csdnimg.cn/direct/15c4c1259c414bc1a6065837220cc4d2.png) ##### 2)C 风格字符串转 string 可以直接用 C 风格字符串初始化 string 对象 ```cpp #include #include using namespace std; int main() { const char* cstr = "Hello"; string s(cstr); cout << "string: " << s << endl; return 0; } ``` ![](https://i-blog.csdnimg.cn/direct/de41e521dec54ea2b93732d8f8973c30.png) #### 小结 string这个类所包含的一些成员函数在很大程度上便捷了我们的编程,譬如,运用C语言定义一个字符数组,需要运用 '\\0' 作为字符串结束标志来表示这是一个字符串,但是C++有很多定义字符串的方式;又譬如在查找特定字符时,C语言需要运用 for 循环或者 while 循环来遍历地找到目标字符,而C++ 中的类 string 有特定的成员函数 find 来找到该目标字符。这不仅实现了代码的优化,还极大地缩短了编程时间。

相关推荐
荒川之神1 小时前
拉链表概念与基本设计
java·开发语言·数据库
chushiyunen1 小时前
python中的@Property和@Setter
java·开发语言·python
小樱花的樱花1 小时前
C++ new和delete用法详解
linux·开发语言·c++
froginwe111 小时前
C 运算符
开发语言
fengfuyao9852 小时前
低数据极限下模型预测控制的非线性动力学的稀疏识别 MATLAB实现
开发语言·matlab
摇滚侠2 小时前
搭建前端开发环境 安装 nodejs 设置淘宝镜像 最简化最标准版本 不使用 NVM NVM 高版本无法安装低版本 nodejs
java·开发语言·node.js
t198751282 小时前
MATLAB十字路口车辆通行情况模拟系统
开发语言·matlab
yyk的萌2 小时前
AI 应用开发工程师基础学习计划
开发语言·python·学习·ai·lua
Amumu121383 小时前
Js:正则表达式(一)
开发语言·javascript·正则表达式
努力的章鱼bro3 小时前
操作系统-FileSystem
c++·操作系统·risc-v·filesystem