当前位置: 首页 > article >正文

string的详细用法c++

std::string 是 C++ 中用于处理字符串的类,提供了丰富的成员函数和操作符来方便地操作字符串。以下是 std::string 的详细用法及示例:


1. 基本用法

1.1 创建和初始化

#include <iostream>
#include <string>

int main() {
    // 默认初始化
    std::string s1;

    // 用C风格字符串初始化
    std::string s2 = "Hello";

    // 用另一个std::string初始化
    std::string s3(s2);

    // 用字符重复初始化
    std::string s4(5, 'A'); // "AAAAA"

    std::cout << "s1: " << s1 << std::endl; // 空字符串
    std::cout << "s2: " << s2 << std::endl; // Hello
    std::cout << "s3: " << s3 << std::endl; // Hello
    std::cout << "s4: " << s4 << std::endl; // AAAAA

    return 0;
}

1.2 访问字符

可以通过下标或 at() 方法访问字符串中的字符。

#include <iostream>
#include <string>

int main() {
    std::string s = "Hello";

    // 使用下标访问
    std::cout << "First character: " << s[0] << std::endl; // H

    // 使用at()访问
    std::cout << "Second character: " << s.at(1) << std::endl; // e

    // 修改字符
    s[0] = 'h';
    std::cout << "Modified string: " << s << std::endl; // hello

    return 0;
}

1.3 字符串长度

使用 size()length() 获取字符串的长度。

#include <iostream>
#include <string>

int main() {
    std::string s = "Hello, World!";

    std::cout << "Length: " << s.length() << std::endl; // 13
    std::cout << "Size: " << s.size() << std::endl;     // 13

    return 0;
}

1.4 字符串拼接

使用 +append() 拼接字符串。

#include <iostream>
#include <string>

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

    // 使用 + 拼接
    std::string s3 = s1 + ", " + s2 + "!";
    std::cout << s3 << std::endl; // Hello, World!

    // 使用 append() 拼接
    s1.append(", ").append(s2).append("!");
    std::cout << s1 << std::endl; // Hello, World!

    return 0;
}

1.5 字符串比较

使用 ==, !=, <, >, <=, >=compare() 比较字符串。

#include <iostream>
#include <string>

int main() {
    std::string s1 = "apple";
    std::string s2 = "banana";

    if (s1 == s2) {
        std::cout << "s1 == s2" << std::endl;
    } else if (s1 < s2) {
        std::cout << "s1 < s2" << std::endl; // 输出
    } else {
        std::cout << "s1 > s2" << std::endl;
    }

    // 使用 compare()
    int result = s1.compare(s2);
    if (result == 0) {
        std::cout << "s1 == s2" << std::endl;
    } else if (result < 0) {
        std::cout << "s1 < s2" << std::endl; // 输出
    } else {
        std::cout << "s1 > s2" << std::endl;
    }

    return 0;
}

2. 常用成员函数

2.1 substr()

提取子字符串。

#include <iostream>
#include <string>

int main() {
    std::string s = "Hello, World!";

    // 提取从索引7开始的5个字符
    std::string sub = s.substr(7, 5);
    std::cout << "Substring: " << sub << std::endl; // World

    return 0;
}

2.2 find()

查找子字符串或字符。

#include <iostream>
#include <string>

int main() {
    std::string s = "Hello, World!";

    // 查找子字符串
    size_t pos = s.find("World");
    if (pos != std::string::npos) {
        std::cout << "Found at index: " << pos << std::endl; // 7
    } else {
        std::cout << "Not found!" << std::endl;
    }

    // 查找字符
    pos = s.find('o');
    if (pos != std::string::npos) {
        std::cout << "Found 'o' at index: " << pos << std::endl; // 4
    }

    return 0;
}

2.3 replace()

替换子字符串。

#include <iostream>
#include <string>

int main() {
    std::string s = "Hello, World!";

    // 替换从索引7开始的5个字符
    s.replace(7, 5, "C++");
    std::cout << "After replace: " << s << std::endl; // Hello, C++!

    return 0;
}

2.4 erase()

删除子字符串或字符。

#include <iostream>
#include <string>

int main() {
    std::string s = "Hello, World!";

    // 删除从索引5开始的6个字符
    s.erase(5, 6);
    std::cout << "After erase: " << s << std::endl; // Hello!

    return 0;
}

2.5 insert()

插入字符串或字符。

#include <iostream>
#include <string>

int main() {
    std::string s = "Hello, !";

    // 在索引7处插入字符串
    s.insert(7, "World");
    std::cout << "After insert: " << s << std::endl; // Hello, World!

    return 0;
}

2.6 clear()

清空字符串。

#include <iostream>
#include <string>

int main() {
    std::string s = "Hello, World!";

    s.clear();
    std::cout << "After clear: " << s << std::endl; // 空字符串

    return 0;
}

2.7 empty()

检查字符串是否为空。

#include <iostream>
#include <string>

int main() {
    std::string s = "Hello";

    if (s.empty()) {
        std::cout << "String is empty!" << std::endl;
    } else {
        std::cout << "String is not empty!" << std::endl; // 输出
    }

    return 0;
}

3. 其他常用操作

3.1 遍历字符串

#include <iostream>
#include <string>

int main() {
    std::string s = "Hello";

    // 使用下标遍历
    for (size_t i = 0; i < s.size(); i++) {
        std::cout << s[i] << " "; // H e l l o
    }
    std::cout << std::endl;

    // 使用范围for循环遍历
    for (char c : s) {
        std::cout << c << " "; // H e l l o
    }
    std::cout << std::endl;

    return 0;
}

3.2 字符串与数字的转换

#include <iostream>
#include <string>

int main() {
    // 字符串转整数
    std::string s1 = "123";
    int num = std::stoi(s1);
    std::cout << "Number: " << num << std::endl; // 123

    // 整数转字符串
    int n = 456;
    std::string s2 = std::to_string(n);
    std::cout << "String: " << s2 << std::endl; // 456

    return 0;
}


http://www.kler.cn/a/529499.html

相关文章:

  • 使用 DeepSeek-R1 等推理模型将 RAG 转换为 RAT,以实现更智能的 AI
  • UE编辑器工具
  • Gradle配置指南:深入解析settings.gradle.kts(Kotlin DSL版)
  • 手写call函数、手写apply函数、手写bind函数
  • 拉格朗日定理
  • 省级-新质生产力数据(2010-2022年)-社科数据
  • R绘图 | pheatmap 聚类并设置间隔
  • O3 模型正式上线,能否与 DeepSeek 一较高下?
  • ubuntu直接运行arm环境qemu-arm-static
  • C++语法·十伞
  • git基础使用--3---git安装和基本使用
  • 属性编程与权限编程
  • 【python】python油田数据分析与可视化(源码+数据集)【独一无二】
  • 高斯光束介绍及光斑处理
  • java-抽象类注意点
  • python学opencv|读取图像(五十四)使用cv2.blur()函数实现图像像素均值处理
  • C++ 泛型编程指南02 (模板参数的类型推导)
  • 【Block总结】门控轴向注意力Gated Axial-Attention|即插即用
  • 代码练习2.3
  • 为何 git 默认是 master分支,而github默认是main分支(DeepSeek问答)
  • MiniMax:人工智能领域的创新先锋
  • git安装flutter
  • 【含文档+PPT+源码】基于微信小程序的校园快递平台
  • SpringBoot 整合 SpringMVC:SpringMVC的注解管理
  • 封装 JDK 自带的 HttpServer
  • 笔记:电机系统性能标定测试怎么进行?