C++系列十五:字符串

C++中的字符串是由字符组成的序列。字符串常用于处理文本数据,例如用户输入、文件内容等。C++标准库提供了一个名为std::string的类,用于表示和处理字符串。

1 、创建和初始化C++字符串

在C++中,可以使用多种方式创建和初始化字符串。以下是一些常用的方法:

(1)直接赋值

std::string str = "Hello, World!";

(2)使用构造函数

std::string str("Hello, World!");

(3) 使用字符数组

char arr[] = "Hello, World!";
std::string str(arr);

(4) 使用字符指针

const char* ptr = "Hello, World!";
std::string str(ptr);

2. C++字符串的常用操作

(1)获取字符串长度

使用size()函数可以获取字符串的长度。

std::string str = "Hello, World!";
int length = str.size(); // 长度为13

(2) 连接字符串

使用+运算符或append()函数可以连接两个字符串。

std::string str1 = "Hello";
std::string str2 = "World!";
std::string str3 = str1 + str2; // 结果为"HelloWorld!"
str3.append(str2); // 结果仍为"HelloWorld!"

(3) 访问字符串中的字符

使用索引运算符[]at()函数可以访问字符串中的字符。注意,索引从0开始。

std::string str = "Hello, World!";
char firstChar = str[0]; // 结果为'H'
char secondChar = str.at(1); // 结果为'e',at()函数也可以用于访问字符串中的字符

(4) 字符串比较

使用==!=<>等运算符进行字符串比较。

std::string str1 = "Hello";
std::string str2 = "World";
if (str1 == str2) {
   
    std::cout << "str1 and str2 are equal";
} else {
   
    std::cout << "str1 and str2 are not equal";
}

3. C++字符串处理函数

(1)字符串切片:substr()函数

std::string str = "Hello, World!";
std::string subStr = str.substr(0, 5); // 结果为"Hello"

(2)字符串替换:replace()函数

std::string str = "Hello, World!";
str.replace(0, 5, "Hi"); // 结果为"Hi, World!"

(3)字符串分割:find()substr()函数

std::string str = "Hello, World!";
size_t pos = str.find(",");
std::string part1 = str.substr(0, pos); // 结果为"Hello"
std::string part2 = str.substr(pos + 1); // 结果为" World!"

4. C++字符串在实际开发中的应用

字符串在C++中广泛应用于各种场景,例如用户输入处理、文件操作、网络通信等。以下是一些示例:

(1)用户输入处理

使用字符串接收用户输入,并进行相应的处理。

#include <iostream>
#include <string>

int main() {
   
    std::string input;
    std::cout << "Enter your name: ";
    std::getline(std::cin, input); // 读取一行输入到字符串中
    std::cout << "Hello, " << input << "!"; // 输出问候语
    return 0;
}

(2)文件操作

使用字符串读取或写入文件路径、文件名等。

#include <fstream>
#include <iostream>
#include <string>

int main() {
   
    std::string filename = "example.txt"; // 文件名或路径
    std::ofstream outfile(filename); // 创建输出文件流,打开文件进行写入操作
    outfile << "Hello, World!"; // 写入内容到文件中
    outfile.close(); // 关闭文件流,完成写入操作
    return 0;
}

(3)网络通信

在处理网络请求或响应时,通常需要使用字符串来表示和处理文本数据。

#include <iostream>
#include <string>
#include <curl/curl.h>

size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp) {
   
    std::stringbuf buf((char*)contents, size * nmemb);
    std::string str = std::string(buf.data(), size * nmemb);
    std::cout << str; // 输出接收到的文本数据
    return size * nmemb;
}

int main() {
   
    CURL *curl = curl_easy_init();
    if (curl) {
   
        std::string url = "http://example.com"; // 请求的URL
        curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
        curl_easy_perform(curl); // 发送请求并接收响应
        curl_easy_cleanup(curl);
    }
    return 0;
}

(4)字符串格式化

使用字符串来组织和呈现数据,例如日期、时间、货币等。

#include <iostream>
#include <sstream>
#include <iomanip>
#include <ctime>

int main() {
   
    std::time_t now = std::time(0); // 获取当前时间
    std::tm* localTime = std::localtime(&now); // 转换为本地时间
    std::ostringstream oss; // 创建输出字符串流
    oss << "Today is " << std::put_time(localTime, "%A %B %d, %Y"); // 格式化输出时间字符串
    std::string formattedDate = oss.str(); // 获取格式化后的字符串
    std::cout << formattedDate; // 输出格式化后的日期字符串
    return 0;
}

相关推荐

  1. C++系列字符串

    2024-01-11 01:54:03       48 阅读
  2. 【重学C语言】三、字符串

    2024-01-11 01:54:03       32 阅读
  3. c++ STL系列——()map

    2024-01-11 01:54:03       45 阅读
  4. C语言经典面试题目(

    2024-01-11 01:54:03       42 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-11 01:54:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-11 01:54:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-11 01:54:03       82 阅读
  4. Python语言-面向对象

    2024-01-11 01:54:03       91 阅读

热门阅读

  1. TensorRT加速推理入门-1:Pytorch转ONNX

    2024-01-11 01:54:03       44 阅读
  2. 神经网络中的损失函数(上)——回归任务

    2024-01-11 01:54:03       45 阅读
  3. vue element plus Form 表单

    2024-01-11 01:54:03       58 阅读
  4. Redis 为什么是单线程的?

    2024-01-11 01:54:03       62 阅读
  5. 65、python - 利用手写的网络,成功预测一张图片

    2024-01-11 01:54:03       60 阅读
  6. 【LintCode】920.会议室

    2024-01-11 01:54:03       57 阅读
  7. #Uniapp:uni-app中vue2生命周期--11个

    2024-01-11 01:54:03       57 阅读
  8. LeetCode每周五题_2024/01/08~01/12

    2024-01-11 01:54:03       83 阅读
  9. 2024.1.8力扣每日一题——回旋镖的数量

    2024-01-11 01:54:03       59 阅读
  10. HDMI2.1 Redriver 信号增强 支持8K60

    2024-01-11 01:54:03       63 阅读
  11. [Microsoft Edge] 如何彻底卸载 Edge

    2024-01-11 01:54:03       50 阅读
  12. 小程序开发之uniapp项目框架搭建

    2024-01-11 01:54:03       63 阅读
  13. VUE +element ui 表格实现数据轮播滚动效果

    2024-01-11 01:54:03       48 阅读
  14. SQL Server 加密 view文本

    2024-01-11 01:54:03       54 阅读