string

1.对象创建

复制代码
//
// Created by 徐昌真 on 2024/12/15.
//
#include <iostream>
#include <string>

using namespace std;

int main() {

    string s = "意思是不要沉迷于空洞的幻想 也不要追求无用的虚名 强调了做事要脚踏实地 专注于实际的目标与行动";

    // 1. 无参构造
    string s1;
    cout << "s1: " << s1 << endl;

    // 2. 初始化列表
    string s2 = {'h','e'};
    cout << "s2: " << s2 << endl;

    // 3. 字符串初始化
    string s3_1 = "不驰于空想";
    string s3_2("不骛于虚声");
    cout << "s3_1: " << s3_1 << endl;
    cout << "s3_2: " << s3_2 << endl;

    // 4. 拷贝构造
    string s4(s);
    cout << "s4: " << s4 << endl;

    // 5. 前n'个字符
    string s5("我是久久", 5);  //具体一个汉字占几个字符 和编码格式有关 字符前位是负数
    cout << "s5: " << s5 << endl;

    // a 个 b
    string s6(10,'a');  //仅限字符
    cout << "s6: " << s6 << endl;


    return 0;
}

输出

2.插入

复制代码
//
// Created by 徐昌真 on 2024/12/18.
//
#include <iostream>
#include <cstring>
#include <string>

using namespace std;


int main() {

    // 1. = +
    string s1 = "不驰于空想";
    s1 = s1 + " 不骛于虚声";
    cout << "s1: " << s1 << endl;

    // 2. +=
    string s2 = "不驰于空想";
    s2 += " 不骛于虚声";
    cout << "s2: " << s2 << endl;

    // 3. append
    // (1)  直接插入(尾部)
    string s3 = "hello";
    s3.append(" world");
    cout << "s3: " << s3 << endl;

    // (2) 插入字符的前n个
    s3.append(" new_one", 4);
    cout << "s3: " << s3 << endl;

    // (3) 从第n个位置开始去m个元素
    s3.append("666 888", 3, 4);
    cout << "s3: " << s3 << endl;
    
    // 4. push_back
    s3.push_back('!');
    cout << "s3: " << s3 << endl;







    return 0;
}

输出

3.随机访问

复制代码
//
// Created by 徐昌真 on 2024/12/18.
//
#include <iostream>
#include <string>

using namespace std;


int main() {

    string s1 = "hello c--";
    cout << "s1: " << s1 << endl;

    // 元素访问
    cout << "s1[0]: " << s1[0] << endl;
    cout << "s1.at(0) " << s1.at(0) << endl;

    // 元素修改
    s1[s1.size() - 2] = '+';
    cout << "s1: " << s1 << endl;
    
    s1.at( s1.size() - 1 ) = '+';
    cout << "s1: " << s1 << endl;



    return 0;
}

4.插入操作

复制代码
//
// Created by 徐昌真 on 2024/12/18.
//
#include <iostream>
#include <string>

using namespace std;

int main() {

    string s1 = "heo";
    cout << "s1: " << s1 << endl;

    // 1. 在n的位置插m个c
    s1.insert(2, 2, 'l' );
    cout << "s1: " << s1 << endl;

    // 2. 在n的位置插入字符串
    s1.insert(5, " world");
    cout << "s1: " << s1 << endl;

    // 3. 迭代器
    s1.insert(s1.end(), '!');
    cout << "s1: " << s1 << endl;
    
    return 0;
}

输出

5.删除操作

复制代码
//
// Created by 徐昌真 on 2024/12/18.
//
#include <iostream>
#include <string>

using namespace std;

int main() {

    string s1 = "hello";
    cout << "s1: " << s1 << endl;

    //erase
    // 1. 全删
    s1.erase();
    cout << "s1: " << s1 << endl;
    cout << endl;

    s1 = "hello";
    cout << "s1: " << s1 << endl;

    // 2. 删除n后面的
    s1.erase(2);
    cout << "s1: " << s1 << endl;
    cout << endl;

    // 3. 删除n往后m个
    s1 = "hello";
    cout << "s1: " << s1 << endl;

    s1.erase(2, 2);
    cout << "s1: " << s1 << endl;
    cout << endl;

    // iterator 删除迭代器位置的
    s1 = "hello";
    cout << "s1: " << s1 << endl;

    s1.erase(s1.begin() );
    cout << "s1: " << s1 << endl;
    cout << endl;


    // iterator 删除迭代器位置的
    s1 = "hello";
    cout << "s1: " << s1 << endl;

    s1.erase(s1.begin() + 2, s1.begin() + 4);  //左闭右开
    cout << "s1: " << s1 << endl;
    cout << endl;


    return 0;
}

输出

6.元素查找

复制代码
//
// Created by 徐昌真 on 2024/12/18.
//
#include <iostream>
#include <string>

using namespace std;

int main() {

    // 默认find
    string s1 = "hello woooooorld";
    cout << "s1.find(\"oooo\"): " << s1.find("oooo") << endl;  //返回所在位置

    // 从第n个位置开始find
    cout << "s1.find('o'): " << s1.find('o') << endl;
    cout << "s1.find('o', 6): " << s1.find('o', 6) << endl;

    // rfind  从右边开始找
    cout << "s1.rfind('o'): " << s1.rfind('o') << endl;

    return 0;
}

7.元素替换

复制代码
//
// Created by 徐昌真 on 2024/12/18.
//
#include <iostream>
#include <string>

using namespace std;


int main() {

    string s1 = "hello java";;
    cout << "s1: " << s1 << endl;

    // replace
    // (1) 从n个位置开始选择n个元素替换掉
    s1.replace( 6, 4,  "c++");
    cout << "s1: " << s1 << endl;

    // 和上面的一样 只不过换成了迭代器
    s1.replace( s1.begin() + 6, s1.begin() + 11, "python");
    cout << "s1: " << s1 << endl;
    
    // 把替换的东西取前n个
    s1.replace( s1.begin() + 6, s1.begin() + 13, "c+++++++++++++++", 3);
    cout << "s1: " << s1 << endl;

    return 0;
}

输出

8.元素比较

compare (-1, 0, 1);

越长的越大 同样长度的从第一个不同元素开始 比较ASCII码

> == <

9. 字符获取

复制代码
//
// Created by 徐昌真 on 2024/12/18.
//
#include <iostream>
#include <string>

using namespace std;

int main() {

    // substr
    string s1 = "hello world";
    cout << s1.substr(0, 5) << endl;

    // 案例 获取字符串前后的东西
    string s2 = "hello&world";
    cout << s2.substr( 0,s2.find('&')) << endl;
    cout << s2.substr(s2.find('&') + 1, s1.size() ) << endl;

    return 0;
}

输出

相关推荐
草莓熊Lotso几秒前
Linux 基础开发工具入门:软件包管理器的全方位实操指南
linux·运维·服务器·c++·人工智能·网络协议·rpc
座山雕~1 分钟前
测试接口-----详细
开发语言·postman
小龙报11 分钟前
算法通关指南:数据结构和算法篇 --- 队列相关算法题》--- 1. 【模板】队列,2. 机器翻译
c语言·开发语言·数据结构·c++·算法·学习方法·visual studio
木木em哈哈18 分钟前
有关于cnb自动化的脚本补全
服务器·开发语言·pygame
星释19 分钟前
Rust 练习册 :Poker与扑克牌游戏
开发语言·游戏·rust
晨非辰28 分钟前
【数据结构初阶】--从排序算法原理分析到代码实现操作,参透插入排序的奥秘!
c语言·开发语言·数据结构·c++·算法·面试·排序算法
Jonathan Star33 分钟前
Next.js、NestJS、Nuxt.js 是 **Node.js 生态中针对不同场景的框架**
开发语言·javascript·node.js
zhangyao9403303 小时前
关于js导入Excel时,Excel的(年/月/日)日期是五位数字的问题。以及对Excel日期存在的错误的分析和处理。
开发语言·javascript·excel
骑驴看星星a4 小时前
【Three.js--manual script】4.光照
android·开发语言·javascript
2301_795167204 小时前
玩转Rust高级应用 如何避免对空指针做“解引用”操作,在C/C++ 里面就是未定义行为
c语言·c++·rust